I'm working with some hardware which has a DLL. I believe it has two methods of calling the functionality (from using a DLL spy app).
It currently works in VB6 using
Public Type Versions
Pname As Long
Manuf As Long
VMajor As Long
VMinor As Long
VState As Long
End Type
Public Declare Function FLASH_GetVersion Lib "C:\flash_cntrl.dll" (ByVal fxc_Versions As Long) As Integer
Dim vVers As Versions
Dim strProdName As String
Dim strManuf As String
strProdName = " "
strManuf = " "
vVers.Manuf = StrPtr(strManuf)
vVers.Pname = StrPtr(strProdName)
iReturn = FLASH_GetVersion(VarPtr(vVers))
I'm being "forced" to move to C# 2008/2010 to be able to use some other hardware which isn't supported in VB6.
So, I'm trying to use the above and running into the AccessViolationException
error. I'm "lucky" enough to have a C++ header file with the WinAPI definitions but still struggling. Any help would be much appreciated.
typedef struct {
LPWSTR productname;
LPWSTR manufacturer;
unsigned Vers_Major;
unsigned Vers_Minor;
unsigned Vers_State; // = 0 released, > 0 internal use only
} FLASH_INFO;
int WINAPI FLASH_GetVersion (FLASH_INFO *fxc_Version);
My C# so far is
static class NativeMethods
{
[DllImport("C:\\flash_cntrl.dll")]
public static extern short FLASH_GetVersion(FLASH_INFO vVers);
}
public class FLASH_INFO
{
public string productname;
public string manufacturer;
public int Vers_Major;
public int Vers_Minor;
public int Vers_State;
}
FLASH_INFO vers = new FLASH_INFO();
string strManuf = " ";
string strProdName = " ";
vers.productname = strProdName;
vers.manufacturer = strManuf;
vers.Vers_Major = 0;
vers.Vers_Minor = 0;
vers.Vers_State = 0;
short sRet = NativeMethods.FLASH_GetVersion(vers);
Console.WriteLine("{0}|{1}", "sRet", sRet.ToString());
It's probably something simple, but I'm relatively new to the C# language (done some desktop programming and silverlight only).
Thank you in advance
string strManuf = " "; string strProdName = " ";
vers.productname = strProdName;
vers.manufacturer = strManuf;
the problem is in those lines. the c++ struct type of them is LPWSTR which translates to IntPtr in .net which is a pointer to an object.
The default marshaling for strings is as 8-bit characters, LPWSTR is a Unicode string though. The return type isn't correct either. Fix:
[DllImport("C:\\flash_cntrl.dll")]
public static extern int FLASH_GetVersion(ref FLASH_INFO vVers);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)
public struct FLASH_INFO
{
public string productname;
public string manufacturer;
public int Vers_Major;
public int Vers_Minor;
public int Vers_State;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.