嗨,我在C ++ MFC中有2个方法,分别调用long* lpHandle
和long lHandle
。我想将这些调用转换为C#。
C ++ MFC示例:
//The api methods
NETCLIENT_API int API_CALL NETCLIENT_MCOpenconfig(long* lpHandle);
NETCLIENT_API int API_CALL NETCLIENT_MCConfig(const char* szDevIDNO,int nType,GPSConfigData_S* pConfig,void* pUsr,void (CALLBACK * FUnmCMsgCB)(GPSMCMsg_S* pMsg,void* pUsr),long lHandle);
...
...
CDlgTransParam::CDlgTransParam(CWnd* pParent /*=NULL*/)
: CDialog(CDlgTransParam::IDD,pParent)
{
//set handle to null
m_lParamHandle = NULL;
}
...
...
void CDlgTransParam::DoConfig(CString strIDNO,GPSConfigData_S* lpInconfig)
{
//opens config handle
NETCLIENT_MCOpenconfig(&m_lParamHandle);
// Sends message; //m_lParamHandle is the return handle of NETCLIENT_MCOpenconfig
NETCLIENT_MCConfig(strIDNO,nType,lpInconfig,this,FUNGetDevConfig,m_lParamHandle);
}
我一直在进行的C#Winform转换:
//Delegate methods
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void FUnmCMsgCB(int nmsg,Form1 form1);
class NETCLASS
{
//--
[DllImport("libnetclient.dll",CallingConvention = CallingConvention.StdCall)]
public static extern int NETCLIENT_MCOpenconfig(_x);
//--
[DllImport("libnetclient.dll",CallingConvention = CallingConvention.StdCall)]
public static extern int NETCLIENT_MCConfig(string szDevIDNO,string pConfig,Form1 form1,FUnmCMsgCB _callback,_x);
}
...
...
private void Sendbutton_Click(object sender,EventArgs e)
{
//opens config handle
int handle = NETCLASS.NETCLIENT_MCOpenconfig(_x);
// Sends message; //m_lParamHandle is the return handle of NETCLIENT_MCOpenconfig
FUnmCMsgCB _callback = new FUnmCMsgCB(FUnmCMsgCB);
int param3 = NETCLASS.NETCLIENT_MCConfig(szDevIDNO,pdata,_callback,_x);
}
在C#示例中,您将看到_x
。根据{{3}}。 C ++ long
在C#中转换为int
。
我试过这样定义:
int str = 0;//set value to zero
GCHandle handlel = GCHandle.Alloc(str,GCHandleType.Weak);//handle type
IntPtr pointer = GCHandle.ToIntPtr(handlel);//define pointer
int lHandle = pointer.ToInt32();//convert to int
它没有用。我还注意到long
类型的变量名是lpHandle,如api方法(e.g "long* lpHandle"
)中所述。在C#中是Intptr
,所以我自然尝试使用Intptr
和ref Intptr
,如:
NETCLIENT_MCOpenconfig(ref IntPtr lHandle);//ref pointer
NETCLIENT_MCOpenconfig(IntPtr lHandle);//normal pointer
仍然没有运气。如果有人有任何建议或提示,我将不胜感激。