读书人

关于C#调用C++程序,该怎么解决

发布时间: 2012-03-23 12:06:21 作者: rapoo

关于C#调用C++程序
我有个C++写的DLL,并提供了.H文件。 其中有个东东在H文件中的定义是这样的:
/*
* typedef:
*PemnCommHandle
*------------------------------
* Description:
* Communication handle to the agent/PEM returned by PemnOpen
*This handle is used with all subsequent call to the agent (e.g
*PemnSend, PemnReceive).
*------------------------------
*/
typedef void *PemnCommHandle;


使用这个东东的方法定义如下:
/*
* Function:
*PemnOpen
*------------------------------
* Purpose :
* Open a connection to a PATROL agent.
*The caller specifies the host name and port number. For other
*communication attributes see PemnSet/GetCommAttributes below.
*
* When the connection is opened successfully the OpenCallback is called.
* If the connection could not be opened then CloseCall back is called.
*
*IMPORTANT pOpenCallback should call the PemnSendIdentity()
* immediatly to authenticate the connection. (V31*)
*------------------------------
* Arguments:
*iPort - port number of the agent/PEM
*pcRemoteHost - Host name of the agent/PEM.
*pCloseCallback, pOpenCallback - PEM API calls these
*routines when the connection with agent/PEM is closed/opened
*pCloseClientData, pOpenClientData - The PEM API passes
*pCloseClientData (resp. pOpenClientData)
*when calling pCloseCallback (resp. pOpenCallback).
*phComm - address in the application of the variable which
*will holds the communication handles.
*The PEM API store internally the address of where to
*return the Comm. handler.
*
*THE VARIABLE HOLDING THIS COMMUNICATION
*HANDLE SHOULD BE A STATIC VARIABLE IN THE PROGRAM.
*
*/
DLL_IMPORT void PemnOpen(
int iPort,
char *pcRemoteHost,
PemnCommCallback pCloseCallback,
PemnClientDatapCloseClientData,
PemnCommCallbackpOpenCallback,
PemnClientDatapOpenClientData,
PemnCommHandle *phComm
);


而我的C#调用代码如下:

public delegate void PemnCommHandle();

[DllImport("pemapi.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern string PemnBOpen(
PemnCommHandle phComm,
string pcHostName,
int iPort,
string pcUserName,
string pcEncryptedPassword,
int iMaxRetry,
PemnBDisconnectCallback pDisconnectCallback,
PemnClientData pClientData
);

public void Temp()
{
Console.WriteLine("PemnCommHandle被调用。");
}


PemnCommHandle pHandle = new PemnCommHandle(Temp);
strOpenResult = PemnBOpen(pHandle, "10.10.198.12", 3181, "patrol", sbEncryptedPwd.ToString(), 1, pCallBack, null);

我想问下:

1. PemnCommHandle 是函数指针么?

2. 我写的C#代码写得对吗,PemnCommHandle对我的程序没什么意义嘛

[解决办法]
你c#中的参数都是什么类型啊,人家c++的是什么类型,你搞清楚了吗,PemnCommCallback 之类的都是什么类型啊,PemnCommHandle 应该就是个句柄,干吗用的你要不知道就问接口提供者
[解决办法]
C#的数据类型和C++的有些是不一样的,比如char [] 和C#里的char []就不一回事


[解决办法]
char* PemnParamAttrArray[PEMN_PARAM_MAX_ATTR];


char* PemnParamAttrArray[10]

这样写的好处是当修改了枚举个数,数组长度也会动态的改变。

读书人网 >C#

热点推荐