我正在尝试调用 c++ 未管理的 dll ref。来自 c# 应用程序的类对象。DLL 函数调用使用 VC++。
我的 VC++ 代码如下
class METHOD_TYPE CDeskApi
{
public:
CDeskApi(void);
/*
Description : Initiates a connection to NEST system
Parameters : 1. serialkey provided to implement the api
2. object of CDeskApiCallback implemented
Return : 0 in case of success and -1 in case of error
*/
int Initialise(char *serialkey, CDeskApiCallback* callback);
/*
Description : Request data from the server
Parameters : 1. symbol of interest
2. intervals of 1 min, multiples of 1 min, DAILY_PERIOD in case of daily.
3. data to be retrieved from. in no of seconds since epoch
4. identifier, which is returned in the callback
Return : 0 in case of success and -1 in case of error
*/
~CDeskApi(void);
};
class METHOD_TYPE CDeskApiCallback
{
public:
};
class Myhandler : public CDeskApiCallback
{
public:
virtual int quote_notify( const char* symbol, int size, Quotation *pQuotes, unsigned long echo)
{
return 0;
};
};
Myhandler handler;
void Cnest_desk_appDlg::OnBnClickedOk()
{
if(odesk.Initialise("VWAP", &handler))
{
AfxMessageBox("Error!");
return;//error
}
}
我的C#代码如下
[DllImport("DeskApi.dll", EntryPoint = "?Initialise@CDeskApi@@QAEHPADPAVCDeskApiCallback@@@Z")]
static extern void DeskApiInitialize(IntPtr symbol, callback fn);
private delegate int callback(IntPtr symbol, int nMaxSize, ref Quotation pQuotes, ulong echo);
private callback mInstance;
private void btnFetch_Click(object sender, EventArgs e)
{
IntPtr ptrCString = (IntPtr)Marshal.StringToHGlobalAnsi(txtFetch.Text);
CallTest.DeskApiGetQuote(ptrCString,quote_notify);
Marshal.FreeHGlobal(ptrCString);
}
private int quote_notify(IntPtr symbol, int nMaxSize, ref Quotation pQuotes, ulong echo)
{
return 0;
}
在 C# 中一切正常,但它不调用quote_notify函数?