locked
How to write/close file in dynamic-link DLL

Answers

  •  Hi all:

    Now, it's ok.

    ==============================================================

    HString hstrKnownFolders;
     hstrKnownFolders.Set(RuntimeClass_Windows_Storage_KnownFolders);

     // Get the Activation Factory
     ComPtr<IActivationFactory> pKnownFoldersActivationFactory;
     hr = GetActivationFactory(hstrKnownFolders.Get(), &pKnownFoldersActivationFactory);
     if (FAILED(hr))
     {
          ::Microsoft::WRL::Details::RaiseException(hr);
     }

     // QI for KnownFolders
     ComPtr<IKnownFoldersStatics> pKnownFolders;
     hr = pKnownFoldersActivationFactory.As(&pKnownFolders);
     if (FAILED(hr))
     {
          ::Microsoft::WRL::Details::RaiseException(hr);
     }

     // Get the Pictures library folder
     ComPtr<IStorageFolder> pStorageFolder;
     //ComPtr<IStorageFile> PanoramaFile;
     ABI::Windows::Storage::IStorageFile* PanoramaFile;

     hr = pKnownFolders->get_PicturesLibrary(&pStorageFolder);
     if (FAILED(hr))
     {
          ::Microsoft::WRL::Details::RaiseException(hr);
     }


     HString hstrFileName;
     hstrFileName.Set(L"sample.txt");

     // Call CreteFileAsync
     __FIAsyncOperation_1_Windows__CStorage__CStorageFile_t * operation;

     hr = pStorageFolder->CreateFileAsync(hstrFileName.Get(),
      CreationCollisionOption::CreationCollisionOption_ReplaceExisting,
      &operation);
     if (FAILED(hr))
     {
          ::Microsoft::WRL::Details::RaiseException(hr);
     }


     operation->GetResults(&PanoramaFile);

     __RPC__deref_out_opt __FIAsyncOperation_1_Windows__CStorage__CStreams__CIRandomAccessStream *RandomStream;

     PanoramaFile->OpenAsync(FileAccessMode_ReadWrite, &RandomStream);

     ComPtr<IOutputStream> spOutputStream;

     ABI::Windows::Storage::Streams::IRandomAccessStream* RAS;

     RandomStream->GetResults(&RAS);

     RAS->GetOutputStreamAt(0, &spOutputStream);

     ComPtr<IDataWriterFactory> spDataWriterFactory;
     ComPtr<IDataWriter> spDataWriter;

        hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_Storage_Streams_DataWriter).Get(),
            spDataWriterFactory.ReleaseAndGetAddressOf());
        if (SUCCEEDED(hr))
        {
             hr = spDataWriterFactory->CreateDataWriter(spOutputStream.Get(),    spDataWriter.ReleaseAndGetAddressOf());
        }

     unsigned char *ImageArrayResult = new unsigned char[5];

     for(int i = 0; i < 5; i++)
      ImageArrayResult[i] = 1;

     __RPC__deref_out_opt __FIAsyncOperation_1_UINT32 *x;
     __RPC__deref_out_opt __FIAsyncOperation_1_boolean *y;

        if (SUCCEEDED(hr))
        {
             spDataWriter->WriteBytes(5, ImageArrayResult);
             spDataWriter->StoreAsync(&x);
             spDataWriter->FlushAsync(&y);
        }

    ================================================================


    • Edited by Hardi_Liang Wednesday, April 11, 2012 3:31 AM
    • Marked as answer by Hardi_Liang Wednesday, April 11, 2012 3:31 AM
    • Unmarked as answer by Hardi_Liang Thursday, April 12, 2012 1:04 PM
    • Proposed as answer by Jesse Jiang Wednesday, April 25, 2012 6:56 AM
    • Marked as answer by Jesse Jiang Monday, May 07, 2012 2:51 AM
    Wednesday, April 11, 2012 2:58 AM

All replies

  •  Hi all:

    Now, it's ok.

    ==============================================================

    HString hstrKnownFolders;
     hstrKnownFolders.Set(RuntimeClass_Windows_Storage_KnownFolders);

     // Get the Activation Factory
     ComPtr<IActivationFactory> pKnownFoldersActivationFactory;
     hr = GetActivationFactory(hstrKnownFolders.Get(), &pKnownFoldersActivationFactory);
     if (FAILED(hr))
     {
          ::Microsoft::WRL::Details::RaiseException(hr);
     }

     // QI for KnownFolders
     ComPtr<IKnownFoldersStatics> pKnownFolders;
     hr = pKnownFoldersActivationFactory.As(&pKnownFolders);
     if (FAILED(hr))
     {
          ::Microsoft::WRL::Details::RaiseException(hr);
     }

     // Get the Pictures library folder
     ComPtr<IStorageFolder> pStorageFolder;
     //ComPtr<IStorageFile> PanoramaFile;
     ABI::Windows::Storage::IStorageFile* PanoramaFile;

     hr = pKnownFolders->get_PicturesLibrary(&pStorageFolder);
     if (FAILED(hr))
     {
          ::Microsoft::WRL::Details::RaiseException(hr);
     }


     HString hstrFileName;
     hstrFileName.Set(L"sample.txt");

     // Call CreteFileAsync
     __FIAsyncOperation_1_Windows__CStorage__CStorageFile_t * operation;

     hr = pStorageFolder->CreateFileAsync(hstrFileName.Get(),
      CreationCollisionOption::CreationCollisionOption_ReplaceExisting,
      &operation);
     if (FAILED(hr))
     {
          ::Microsoft::WRL::Details::RaiseException(hr);
     }


     operation->GetResults(&PanoramaFile);

     __RPC__deref_out_opt __FIAsyncOperation_1_Windows__CStorage__CStreams__CIRandomAccessStream *RandomStream;

     PanoramaFile->OpenAsync(FileAccessMode_ReadWrite, &RandomStream);

     ComPtr<IOutputStream> spOutputStream;

     ABI::Windows::Storage::Streams::IRandomAccessStream* RAS;

     RandomStream->GetResults(&RAS);

     RAS->GetOutputStreamAt(0, &spOutputStream);

     ComPtr<IDataWriterFactory> spDataWriterFactory;
     ComPtr<IDataWriter> spDataWriter;

        hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_Storage_Streams_DataWriter).Get(),
            spDataWriterFactory.ReleaseAndGetAddressOf());
        if (SUCCEEDED(hr))
        {
             hr = spDataWriterFactory->CreateDataWriter(spOutputStream.Get(),    spDataWriter.ReleaseAndGetAddressOf());
        }

     unsigned char *ImageArrayResult = new unsigned char[5];

     for(int i = 0; i < 5; i++)
      ImageArrayResult[i] = 1;

     __RPC__deref_out_opt __FIAsyncOperation_1_UINT32 *x;
     __RPC__deref_out_opt __FIAsyncOperation_1_boolean *y;

        if (SUCCEEDED(hr))
        {
             spDataWriter->WriteBytes(5, ImageArrayResult);
             spDataWriter->StoreAsync(&x);
             spDataWriter->FlushAsync(&y);
        }

    ================================================================


    • Edited by Hardi_Liang Wednesday, April 11, 2012 3:31 AM
    • Marked as answer by Hardi_Liang Wednesday, April 11, 2012 3:31 AM
    • Unmarked as answer by Hardi_Liang Thursday, April 12, 2012 1:04 PM
    • Proposed as answer by Jesse Jiang Wednesday, April 25, 2012 6:56 AM
    • Marked as answer by Jesse Jiang Monday, May 07, 2012 2:51 AM
    Wednesday, April 11, 2012 2:58 AM
  • Thanks for sharing the solution.

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, April 11, 2012 8:44 AM
  • Hello,

    Did you have another question about this problem? If you need further assistance, feel free to let me know. I will be more than happy to be of assistance.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, April 25, 2012 6:57 AM
  • Is there any example code on how to block until StoreAsync operation is completed?

    If I want the result of StoreAsync(), I'll have to call GetResults() on the async operation, but not until it has completed.

    In an app compiled with /ZW, I would just create a task and call GetResults() in the .then() lambda function, but can't figure out how to do this in a DLL.

    Wednesday, May 30, 2012 10:25 PM
  • @Jani L,

    Did you figure this out, I have the same question?  How do you determine when tasks are complete when not using /ZW?

    thanks,

    hrp

    Tuesday, June 05, 2012 5:49 PM
  • Doesn't look like there is any supported good way to do this. If you're running on a thread where you can afford a busy-wait and there is no alternative, you could do something like:

    AsyncStatus status = AsyncStatus::Started;
    while ( status == AsyncStatus::Started )
    {
        Sleep(N);
        if ( FAILED(asyncInfo->get_Status(&status)) )
            return ERR;
    }
    
    ..but this has the obvious downsides of a maximum latency of N for detecting complete and the busy waiting performance hit.

    Tuesday, June 05, 2012 6:01 PM
  • I'm still having a problem with this.  How do I get AsyncInfo on a __FIAsyncOperation_1_Windows__CStorage__CStorageFile_t

    IAsyncOperations are supposed to inherit from IAsyncInfo, but I don't see a get_Status function.  What am I missing?  Screenshot below:

     

     

    Wednesday, July 18, 2012 5:16 PM