Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从c调用c非托管dll中的类ref对象# 我试图从C++应用程序调用C++ uMnangangdDLL Read类对象。使用VC++进行DLL函数调用_C#_C++_Visual C++_Unmanaged - Fatal编程技术网

C# 如何从c调用c非托管dll中的类ref对象# 我试图从C++应用程序调用C++ uMnangangdDLL Read类对象。使用VC++进行DLL函数调用

C# 如何从c调用c非托管dll中的类ref对象# 我试图从C++应用程序调用C++ uMnangangdDLL Read类对象。使用VC++进行DLL函数调用,c#,c++,visual-c++,unmanaged,C#,C++,Visual C++,Unmanaged,我的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 impl

我的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
    }    
}
[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#代码如下

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
    }    
}
[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语言中,一切正常,但它不调用notify函数。

这是完整的代码吗?我对此表示怀疑,因为在CDeskApiCallback中甚至没有任何方法(这可能是用于回调的类似接口的类)。无论哪种方式,C++“回调”对象都与.NET委托有很大的不同,这就是您试图使用它的方式。

另一个主要缺陷是您试图导入一个C++方法(cDeStAPI::Initialise)。这部分工作很好,但是你要如何实例化C++类,以便你可以调用Initialise?


我相信您可以以某种方式使用p/Invoke,但是出于这个目的,COM或C++/CLI会更好。任何COM或C++/CLI类和接口都将对C#直接可见,而无需在C#代码中编写奇怪的编组指令。

是。你对同一个问题是对的。在我的脑海里。但是我对c++/CLI不太了解,所以您可以提供任何链接或示例。谢谢