CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2010
    Posts
    7

    Get string from c++ dll in c#

    Hello.. I got a problem I'm hoping someone here can help me with.
    I am writing an application in c#, and wish to use a c++ library. In this library, I am calling the following method
    Code:
        HANDLE WINAPI SFileFindFirstFile(
          HANDLE hMpq,                      // Archive handle
          const char * szMask,              // Search mask
          SFILE_FIND_DATA * lpFindFileData, // Pointer to the search result
          const char * szListFile           // Name of additional listfile
        );
    Where SFILE_FIND_DATA is defined as
    Code:
        struct SFILE_FIND_DATA
        {
          char   cFileName[MAX_PATH];              // Name of the found file
          char * szPlainName;                      // Plain name of the found file
          DWORD  dwHashIndex;                      // Hash table index for the file
          DWORD  dwBlockIndex;                     // Block table index for the file
          DWORD  dwFileSize;                       // Uncompressed size of the file, in bytes
          DWORD  dwFileFlags;                      // MPQ file flags
          DWORD  dwCompSize;                       // Compressed file size
          DWORD  dwFileTimeLo;                     // Low 32-bits of the file time (0 if not present)
          DWORD  dwFileTimeHi;                     // High 32-bits of the file time (0 if not present)
          LCID   lcLocale;                         // Locale version
        };
    I'm having some trouble getting cFileName as a string in c#.. Anyone got an idea about how to do this?
    What I'm doing now in c# is this
    Code:
        [DllImport("StormLib.dll")]
        private static extern UInt32 SFileFindFirstFile(
              UInt32 hMpq,                      // Archive handle
              byte[] szMask,              // Search mask
              ref SFILE_FIND_DATA lpFindFileData, // Pointer to the search result
              UInt32 szListFile           // Name of additional listfile
        );
    
        struct SFILE_FIND_DATA
        {
                public byte[] cFileName;              // Name of the found file
                public IntPtr szPlainName;                      // Plain name of the found file
                public UInt32 dwHashIndex;                      // Hash table index for the file
                public UInt32 dwBlockIndex;                     // Block table index for the file
                public UInt32 dwFileSize;                       // Uncompressed size of the file, in bytes
                public UInt32 dwFileFlags;                      // MPQ file flags
                public UInt32 dwCompSize;                       // Compressed file size
                public UInt32 dwFileTimeLo;                     // Low 32-bits of the file time (0 if not present)
                public UInt32 dwFileTimeHi;                     // High 32-bits of the file time (0 if not present)
                public UInt32 lcLocale;                         // Locale version
        };
    Like this, I get an exception about an attempt to write protected memory.

    The dll is open source, so if you need more info, I can find it for you, or you can download the code at
    http://www.zezula.net/en/mpq/stormlib.html

  2. #2
    Join Date
    Nov 2010
    Posts
    7

    Re: Get string from c++ dll in c#

    I found a solution.. I needed to change my struct in c# to
    Code:
            struct SFILE_FIND_DATA
            {
                [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 260)]
                public string cFileName;              // Name of the found file
                public IntPtr szPlainName;                      // Plain name of the found file
                public UInt32 dwHashIndex;                      // Hash table index for the file
                public UInt32 dwBlockIndex;                     // Block table index for the file
                public UInt32 dwFileSize;                       // Uncompressed size of the file, in bytes
                public UInt32 dwFileFlags;                      // MPQ file flags
                public UInt32 dwCompSize;                       // Compressed file size
                public UInt32 dwFileTimeLo;                     // Low 32-bits of the file time (0 if not present)
                public UInt32 dwFileTimeHi;                     // High 32-bits of the file time (0 if not present)
                public UInt32 lcLocale;                         // Locale version
            };

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured