DllImport一个c++ DLL到一个c#应用程序,字节* p。

时间:2022-08-31 09:02:32

I have a exported function in a c++ DLL

我在c++ DLL中有一个导出函数。

// C++ DLL (Blarggg.dll)

extern "C"
{
     USHORT ReadProperty( BYTE * messsage, USHORT length, BYTE * invokeID ) 
    {
         if( invokeID != NULL ) {
            * invokeID = 10 ; 
        }
         return 0;
    }
}

That I would like to make it available to my C# application

我想让我的c#应用程序可以使用它。

// C# app 
[DllImport("Blarggg.dll")]
public static extern System.UInt16 ReadProperty(
        /* [OUT] */ System.Byte[] message,
        /* [IN]  */ System.UInt16 length,
        /* [OUT] */ System.Byte[] invokeID ); 


private void DoIt() 
{
    System.Byte[] message = new System.Byte[2000];
    System.Byte[] InvokeID = new System.Byte[1];
    System.UInt16 ret = ReadProperty( message, 2000, InvokeID ); // Error 
}

The problem is that I keep getting the following error message.

问题是我一直得到以下错误消息。

An unhanded exception of type 'System.NullReferenceException' occurred in Blarggg.dll Additional information: Object reference not set to an instance of an object.

类型“系统”的一个未提交的异常。得到NullReferenceException Blarggg发生。dll附加信息:对象引用不设置为对象的实例。

I'm using VS2008 to build both the DLL and the C# application.

我使用VS2008来构建DLL和c#应用程序。

I'm not a C# programmer.

我不是c#程序员。

What am I doing wrong?

我做错了什么?

4 个解决方案

#1


2  

I pasted your code directly into VS2008 and it runs perfectly on my 32-bit machine (added a .def file to set the exported name). Is your C++ library definitely a pure win32 project? The error message you gave seems to imply that it threw a CLR exception.

我将您的代码直接粘贴到VS2008中,并且它在我的32位机器上完美运行(添加了一个.def文件以设置导出的名称)。你的c++库绝对是一个纯win32项目吗?您给出的错误消息似乎暗示它抛出了一个CLR异常。

#2


2  

Try this:

试试这个:

[DllImport("Blarggg.dll", CallingConvention := CallingConvention.Cdecl)] 
public static extern System.UInt16 ReadProperty( 
        /* [IN]  */ System.Byte[] message, 
        /* [IN]  */ System.UInt16 length, 
        /* [OUT] */ out System.Byte invokeID );  


private void DoIt()  
{ 
    System.Byte[] message = new System.Byte[2000]; 
    System.Byte InvokeID; 
    System.UInt16 ret = ReadProperty( message, 2000, out InvokeID );
} 

#3


0  

You might need to use the System.Runtime.InteropServices.Marshal class to convert between managed and unmanaged types.

您可能需要使用System.Runtime.InteropServices。Marshal类,用于在托管类型和非托管类型之间进行转换。

#4


0  

Can you do this with C++ types?

可以用c++类型来做吗?

I was under the impression that you could only DLLImport C dlls.

我当时的印象是,你只能用DLLImport C dlls。

We use DLLImport with C++ Dll's just fine but we declare our external functions as

我们使用带有c++ Dll的DLLImport,但是我们声明了我们的外部函数。

extern "C" __declspec(dllexport) ...

Have a look at this web page:

看看这个网页:

http://dotnetperls.com/dllimport-interop

http://dotnetperls.com/dllimport-interop

#1


2  

I pasted your code directly into VS2008 and it runs perfectly on my 32-bit machine (added a .def file to set the exported name). Is your C++ library definitely a pure win32 project? The error message you gave seems to imply that it threw a CLR exception.

我将您的代码直接粘贴到VS2008中,并且它在我的32位机器上完美运行(添加了一个.def文件以设置导出的名称)。你的c++库绝对是一个纯win32项目吗?您给出的错误消息似乎暗示它抛出了一个CLR异常。

#2


2  

Try this:

试试这个:

[DllImport("Blarggg.dll", CallingConvention := CallingConvention.Cdecl)] 
public static extern System.UInt16 ReadProperty( 
        /* [IN]  */ System.Byte[] message, 
        /* [IN]  */ System.UInt16 length, 
        /* [OUT] */ out System.Byte invokeID );  


private void DoIt()  
{ 
    System.Byte[] message = new System.Byte[2000]; 
    System.Byte InvokeID; 
    System.UInt16 ret = ReadProperty( message, 2000, out InvokeID );
} 

#3


0  

You might need to use the System.Runtime.InteropServices.Marshal class to convert between managed and unmanaged types.

您可能需要使用System.Runtime.InteropServices。Marshal类,用于在托管类型和非托管类型之间进行转换。

#4


0  

Can you do this with C++ types?

可以用c++类型来做吗?

I was under the impression that you could only DLLImport C dlls.

我当时的印象是,你只能用DLLImport C dlls。

We use DLLImport with C++ Dll's just fine but we declare our external functions as

我们使用带有c++ Dll的DLLImport,但是我们声明了我们的外部函数。

extern "C" __declspec(dllexport) ...

Have a look at this web page:

看看这个网页:

http://dotnetperls.com/dllimport-interop

http://dotnetperls.com/dllimport-interop