locked
can we print/log messages in a file in the App-local folder through the native dll (linked to the app)?

    Question

  • I have a metro app that is linked to a native dll. I am trying to log messages in a file at the app-local folder through the native dll by hard coding the path to C:\\users\\abc\\AppData\\Local\\Packages\\89a09666-b5f0-4213-bca0-b73fa8e0290f_z3v4ta9s4nm36\\TempState\\a.txt" .

    However, no file is created when the app is run. Is the approach wrong? DOes the path need to be modified?

    My ultimate aim is to debug the dll, which I am unable to do as the debug version of the dll fails to load (I get the exception "System.DllNotFoundException: Unable to load DLL 'ABC.dll': Invalid access to memory location. (Exception from HRESULT: 0x800703E6)". The release version of the dll however is successfully getting loaded.

    Then I tried this method of logging in a file, which is also failing. Is there a solution to this?

    Thanks in advance!

    Thursday, March 29, 2012 11:26 AM

Answers

  • Hello,

    Yes, you needn't special setting in manifest for accessing the app-local folder.

    You can create a text file in this codes

    StorageFolder^ localFolder = ApplicationData::Current->LocalFolder;
    
    	concurrency::task<StorageFile^> fileOperation = task<StorageFile^> (
    		localFolder->CreateFileAsync("dataFile.txt",Windows::Storage::CreationCollisionOption::ReplaceExisting));
    	fileOperation.then([this](StorageFile^ sampleFile)
    	{
    		auto calendar = ref new Calendar;
    		auto now = calendar->ToDateTime();
    		auto formatter = ref new Windows::Globalization::DateTimeFormatting::DateTimeFormatter("longtime");
    
    		return FileIO::WriteTextAsync(sampleFile, formatter->Format(now));
    	}).then([this](task<void> previousOperation) {
    		try {
    			previousOperation.get();
    		} catch (Platform::Exception^) {
    			// Timestamp not written
    		}
    	});
    In this codes you can access local folder and create a log file.

    Best regards,
    Jesse


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


    • Proposed as answer by Jesse Jiang Thursday, April 05, 2012 8:14 AM
    • Marked as answer by Jesse Jiang Monday, April 09, 2012 1:33 AM
    • Edited by Jesse Jiang Tuesday, April 10, 2012 6:55 AM
    Tuesday, April 03, 2012 8:16 AM

All replies

  • Hello,

    First, How did you open and write txt in your native dll? You can share the codes with us.

    Second, did your forget to add File Type Associations in your metro project, which include the native DLL?
    In your metro project double click Package.appxmanifest->Declarations->Select File Type Associations in Available Declarations and add it. Add New in Supported File Types, enter File Type .txt.

    Best regards,
    Jesse


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

    Friday, March 30, 2012 3:31 AM
  • Hello Jesse,

    Thanks a lot for your response.

    I used fopen and fprintf to write text in my native dll.

    I was unaware of the modification suggested by you in the appmanifest file. However, I have a query here, if I am accessing the app-local folder (which is by default accessible to the app, please refer to the path in my post above), do I still require to do these changes in the app manifest file?

    Thanks,

    Upasana

    Friday, March 30, 2012 11:29 AM
  • Hello,

    Yes, you needn't special setting in manifest for accessing the app-local folder.

    You can create a text file in this codes

    StorageFolder^ localFolder = ApplicationData::Current->LocalFolder;
    
    	concurrency::task<StorageFile^> fileOperation = task<StorageFile^> (
    		localFolder->CreateFileAsync("dataFile.txt",Windows::Storage::CreationCollisionOption::ReplaceExisting));
    	fileOperation.then([this](StorageFile^ sampleFile)
    	{
    		auto calendar = ref new Calendar;
    		auto now = calendar->ToDateTime();
    		auto formatter = ref new Windows::Globalization::DateTimeFormatting::DateTimeFormatter("longtime");
    
    		return FileIO::WriteTextAsync(sampleFile, formatter->Format(now));
    	}).then([this](task<void> previousOperation) {
    		try {
    			previousOperation.get();
    		} catch (Platform::Exception^) {
    			// Timestamp not written
    		}
    	});
    In this codes you can access local folder and create a log file.

    Best regards,
    Jesse


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


    • Proposed as answer by Jesse Jiang Thursday, April 05, 2012 8:14 AM
    • Marked as answer by Jesse Jiang Monday, April 09, 2012 1:33 AM
    • Edited by Jesse Jiang Tuesday, April 10, 2012 6:55 AM
    Tuesday, April 03, 2012 8:16 AM
  • Hi Jesse:

    Do you know how to include (using namespace Windows::Storage;) for dynamic link library(DLL with dllmain)?

    When I use the Storage API. There are build errors.

    Thanks,

    BR,

    Hardi

    Monday, April 09, 2012 1:09 PM
  • Hello,

    I would suggest you to create a WinRT Component DLL project, not the DLL project in Win32. So that you can include that namespace easily.

    Best regards,
    Jesse


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

    Tuesday, April 10, 2012 6:36 AM