C # calls the C++dll package with a series of problems "Go"

Source: Internet
Author: User

Recently to help the bottom of the development of the same time using C # to re-encapsulate the DLL, that is, C # class to encapsulate the method in C++dll, for the user to use.

Before the use of similar applications, most of the problems are in the type conversion, but this application is endless, so here to summarize, for their own future inspection, but also hope that we can help.

  

First, repeat some basic usage methods. Specific of those ways in here will not repeat, online a lot of. Like http://blog.csdn.net/sunboyljp/archive/2009/12/31/5110639.aspx.

Definitions in the C + + header file:

Npd_api int np_init ();

Defining functions in C #

[DllImport ("Npd_api.dll")]

public static extern int Np_init ();

The basic type conversions are shown in the table below (I've used them):

Bstr--stringbuilder

Lpctstr--stringbuilder

Lpcwstr--intptr

Handle--intptr

Hwnd--intptr

Char *--string

int *--ref int

int &--ref int

void *--intptrs

unsigned char *--ref byte

Bool--bool

Dword--uint or int (I'm using uint, nothing's wrong)

My question has come, long lessons I know:

1, the pointer to do the parameters in C # must use the ref or out keyword, especially the structure of pointers, to not report memory read errors, even if the error data is not correct. Oh

Sipclient_api void WINAPI sccleanup (sipclient * PSIP);

[DllImport ("Sipclient.dll")]
public static extern void Sccleanup (ref sipclient PSIP);

Where Sipclient is a struct.

2, when overriding the structure, there are previously specified type length or array length of the place, but also to make the corresponding label, or it will lead to memory errors.

typedefstruct{

     Charsdvrip[16];/*DVR IP Address*/

     Char sdvripmask[]; / * DVR IP address Mask * /

DWORD Dwnetinterface; / * 10m/100m Adaptive, Index * /

WORD Wdvrport; / * port number * /

BYTE Bymacaddr[macaddr_len]; / * Physical Address of server * /

}net_posa_ethernet;



   Public structNet_posa_ethernet
{
[MarshalAs (UnmanagedType.ByValTStr, SizeConst= 16)]
Public StringSdvrip;//DVR IP Address
[MarshalAs (UnmanagedType.ByValTStr, SizeConst= 16)]
Public StringSdvripmask;//DVR IP Address Mask
Public UIntDwnetinterface;//Network interface 1-10mbase-t 2-10mbase-t full-duplex 3-100mbase-tx 4-100m Full-duplex 5-10m/100m adaptive
Public uint Wdvrport; // port number
[MarshalAs (UnmanagedType.ByValArray, SizeConst =6 public byte["BYMACADDR; //[macaddr_len "; //pppoe user name // Physical address of the server
"



3, encounter such a problem, toss a big half day time--http://space.cnblogs.com/q/16616/.

Finally, the C + + has been modified to resolve, by the development of a module definition (. def) file, the unified development of the name of the exported function. The function with the return value as a struct pointer can also be used with IntPtr.

sipclient_api sipclient* scinit (const char * reaml,   Const char * From_ip, int From_port,   Const char * To_ip, int To_port, constchar * server_id, Const char * user_id, const char * user_name, void * user_obj_param);


[DllImport ("Sipclient.dll")]
Public static extern IntPtr Scinit (string Reaml, string From_ip, int From_port,
To_ip, int to_ Port, string server_id,
string user_id, string user_name, IntPtr User_obj_param);

4, but also encountered a callback function caused by the crash problem, but also delayed a large half a day, work and delay will finally find a solution to the hair.

Colleagues have just started to analyze the cause of the crash, are all the disaster of recycling, see Http://www.hudong.com/wiki/WINAPI, try to use __stdcall, but still did not solve the problem

Later practice proved that the procedure is very rigorous, the slightest mistake can not lead to errors, ideas or __stdcall, but less change things, there are two places need to change, to ensure that there is no error.

Refer to http://hi.baidu.com/tease/blog/item/1fe7213802780f22b9998f5a.html.

The key is these two words

typedef void (_stdcall *cicicallback) (bool started, void* Client,char *message);
Modify the exported function to:
extern "C" _declspec (dllexport) bool _stdcall Test (char* fileName, Cicicallback callback)

The first time only modified the definition that, but forgot to export the changes, almost gave up this solution, but fortunately, the so-called insistence is victory!

  
5, later packaged to get the user there, but always hint that C + + those DLLs are not found.

On-line search, preliminary positioning is caused by the development environment, and environmental deployment has a relationship. Our development environment is VS2008, and customers use the vs2010, through a few attempts, the problem finally.

The first consideration is the lack of some C + + prerequisites for the runtime, which is interdependent, so the DLL cannot be found. Look at Dependency Walker to see that there is really something missing on the client's child.

But blocked, copying the missing stuff into the executable directory, the problem remains unresolved. But still hold on to this road ~

The problem persists if you try to install Vcredist_x86.exe to exclude the possibility that some runtimes are still missing.

Later I thought of the search problem before, see as if the DLL version of the Releas\debug also have a relationship, all try to propose to let colleagues change their c++dll to release version.

Because the project is a lot of people to do together, compiled release version also spent a lot of time, but somehow the problem finally solved!

Summary: Install Vcredist_x86.exe directly, all DLLs must use the release version. If you use the debug version, you must ensure that the executable directory DLL is complete, indispensable!

On-Line detailed explanation also many, feels this summarizes very well http://hi.baidu.com/fairysky/blog/item/e7a8366dbaa735f3431694c8.html.

Do the procedure is afraid of problems, the problem is afraid do not know the reason, know the reason is easy to find a solution!

Original: http://www.cnblogs.com/wdysunflower/archive/2010/09/01/1813947.html

C # calls the C++dll package with a series of problems "Go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.