C# 访问本机 C++ DLL
C# Access To Native C++ DLL
我正在尝试创建一个访问本机 C++ DLL 的程序。下面是示例代码。
C++代码
GetRmaPin (const char *rma_password, const char *serial, unsigned char *rma_pin);
C#代码
class Program
{
static void Main ( string [ ] args )
{
string[] password = { "74f3d3a287cee548c1842c07090d6a274dd0ddbd04bfd1e4694861a369bc7304" };
string[] serial = { "184393900006" };
//StringBuilder rma_pin = new StringBuilder(2048);
byte[] rma_pin = new byte[2048];
int rc = GetRmaPin(password, serial, ref rma_pin);
Console.WriteLine ( "Result: " + rc.ToString ( ) );
Console.WriteLine ( "Payload: " + rma_pin.ToString ( ) );
Console.Read ( );
}
[DllImport ( "Security.dll" , EntryPoint = "GetRmaPin" , CallingConvention = CallingConvention.Cdecl)]
public static extern int GetRmaPin (
[In][MarshalAs ( UnmanagedType.LPArray , ArraySubType = UnmanagedType.LPStr )] string [ ] password ,
[In][MarshalAs ( UnmanagedType.LPArray , ArraySubType = UnmanagedType.LPStr )] string [ ] serial ,
ref byte[] rmap_in );
}
错误信息:
托管调试助手 'FatalExecutionEngineError'
Message=托管调试助手 'FatalExecutionEngineError' : 'The runtime has encountered a fatal error. The address of the error was at 0x732dc93d, on thread 0x97a4. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.'
经过一些试验,我找到了一种从第三个参数获取值的方法。这是更新后的代码。感谢@HansPassant 提供删除 "ref" 的提示。希望这对以后的人有帮助。
class Program
{
static void Main ( string [ ] args )
{
string[] password = { "74f3d3a287cee548c1842c07090d6a274dd0ddbd04bfd1e4694861a369bc7304" };
string[] serial = { "184393900006" };
byte[] rma_pin = new byte[32];
int rc = GetRmaPin(password, serial, rma_pin);
Console.WriteLine ( "Result: " + rc.ToString ( ) );
Console.WriteLine ( "Payload: " + BitConverter.ToString ( rma_pin ).Replace ( "-" , "" ) );
Console.Read ( );
}
[DllImport ( "SecurityProduction.dll" , EntryPoint = "GetRmaPin" , CallingConvention = CallingConvention.Cdecl)]
public static extern int GetRmaPin (
[In][MarshalAs ( UnmanagedType.LPArray , ArraySubType = UnmanagedType.LPStr )] string [ ] password ,
[In][MarshalAs ( UnmanagedType.LPArray , ArraySubType = UnmanagedType.LPStr )] string [ ] serial ,
byte[] rmap_in );
}
我正在尝试创建一个访问本机 C++ DLL 的程序。下面是示例代码。
C++代码
GetRmaPin (const char *rma_password, const char *serial, unsigned char *rma_pin);
C#代码
class Program
{
static void Main ( string [ ] args )
{
string[] password = { "74f3d3a287cee548c1842c07090d6a274dd0ddbd04bfd1e4694861a369bc7304" };
string[] serial = { "184393900006" };
//StringBuilder rma_pin = new StringBuilder(2048);
byte[] rma_pin = new byte[2048];
int rc = GetRmaPin(password, serial, ref rma_pin);
Console.WriteLine ( "Result: " + rc.ToString ( ) );
Console.WriteLine ( "Payload: " + rma_pin.ToString ( ) );
Console.Read ( );
}
[DllImport ( "Security.dll" , EntryPoint = "GetRmaPin" , CallingConvention = CallingConvention.Cdecl)]
public static extern int GetRmaPin (
[In][MarshalAs ( UnmanagedType.LPArray , ArraySubType = UnmanagedType.LPStr )] string [ ] password ,
[In][MarshalAs ( UnmanagedType.LPArray , ArraySubType = UnmanagedType.LPStr )] string [ ] serial ,
ref byte[] rmap_in );
}
错误信息: 托管调试助手 'FatalExecutionEngineError' Message=托管调试助手 'FatalExecutionEngineError' : 'The runtime has encountered a fatal error. The address of the error was at 0x732dc93d, on thread 0x97a4. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.'
经过一些试验,我找到了一种从第三个参数获取值的方法。这是更新后的代码。感谢@HansPassant 提供删除 "ref" 的提示。希望这对以后的人有帮助。
class Program
{
static void Main ( string [ ] args )
{
string[] password = { "74f3d3a287cee548c1842c07090d6a274dd0ddbd04bfd1e4694861a369bc7304" };
string[] serial = { "184393900006" };
byte[] rma_pin = new byte[32];
int rc = GetRmaPin(password, serial, rma_pin);
Console.WriteLine ( "Result: " + rc.ToString ( ) );
Console.WriteLine ( "Payload: " + BitConverter.ToString ( rma_pin ).Replace ( "-" , "" ) );
Console.Read ( );
}
[DllImport ( "SecurityProduction.dll" , EntryPoint = "GetRmaPin" , CallingConvention = CallingConvention.Cdecl)]
public static extern int GetRmaPin (
[In][MarshalAs ( UnmanagedType.LPArray , ArraySubType = UnmanagedType.LPStr )] string [ ] password ,
[In][MarshalAs ( UnmanagedType.LPArray , ArraySubType = UnmanagedType.LPStr )] string [ ] serial ,
byte[] rmap_in );
}