从C#程序访问C ++ DLL

时间:2018-09-23 18:55:10

标签: c# interop dllimport

我对互操作非常陌生,在定义从C ++ DLL导入dll时遇到问题。 DLL的文档如下:

bool __stdcall __declspec(dllexport) InitHW(char *name, char *model, int& type)

所以我尝试的代码如下,它给出了system.AccessViolation异常:

[DllImport("extIO_IC7610.dll", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern bool InitHW(string name, string model, int type);

private unsafe void Initialize()
{
    try
    {
        bool result;

        string name = "Test";
        string model = "Model";
        int type = 3;

        result = InitHW(name, model, type);
    }
    catch (Exception ex)
    {

    }
}

我刚刚意识到这应该返回数据。 有人可以在这里告诉我我所理解的错误吗? 谢谢汤姆

根据评论,我将内容更改为以下形式:

[DllImport("extIO_IC7610.dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)] 
public unsafe static extern bool InitHW(string name, string model, ref int type);

unsafe private void Initialize())
{

    try
    {
        bool result;

        string name = ""; 
        string model = ""; 
        int type = 3;
        result = InitHW(name, model, ref type);

    }
    catch (Exception ex)
    {

    }
}

这仍然不起作用。我现在收到一个错误,由于签名不匹配,堆栈不平衡。我认为字符串正确完成了,但&int参数可能仍然是一个问题。 汤姆

1 个答案:

答案 0 :(得分:0)

我解决了。这里有一些提示,但没有真正正确的提示。在环顾了最近几个小时后,这是解决方案:

#include <pthread.h>
#include <iostream>
#include <vector>
#define OK      0
#define ERROR   -1

//-- ThreadClass
class MyThreadClass
{
public:
   MyThreadClass() {/* empty */}
   virtual ~MyThreadClass() {/* empty */}

   /** Returns true if the thread was successfully started, false if there was an error starting the thread */
   bool StartInternalThread()
   {
      return (pthread_create(&_thread, NULL, InternalThreadEntryFunc, this) == 0);
   }

   /** Will not return until the internal thread has exited. */
   void WaitForInternalThreadToExit()
   {
      (void) pthread_join(_thread, NULL);
   }

protected:
   /** Implement this method in your subclass with the code you want your thread to run. */
   virtual void InternalThreadEntry() = 0;

private:
   static void * InternalThreadEntryFunc(void * This) {
       ((MyThreadClass *)This)->InternalThreadEntry(); return NULL;
       }

   pthread_t _thread;
};
//-- /ThreadClass
//--- DUMMY DECLARATIONS BELOW TO MAKE IT COMPILE ---//
#define LOG_NS_ERROR std::cout
class test{
    public:
        int get_child(std::string x){return OK;};
};
test *_global;
typedef struct test_struct{} _db_transact;
class db_transact{
    public: 
        db_transact(int,int&,int&){};
};
int _ns;
int _log_id;
//--- DUMMY DECLARATIONS ABOVE TO MAKE IT COMPILE ---//
class db_c_hndlr : public MyThreadClass{
    public: 
        db_c_hndlr(void);
        ~db_c_hndlr(void);
        db_transact *db_conn_get(void);
        void InternalThreadEntry(void *func);
    private:
        int _stop;
        std::vector<db_transact*> _db_pool;
};
//---------------------------------------------------------

db_c_hndlr::db_c_hndlr(void) {
}
//---------------------------------------------------------

void db_c_hndlr::InternatThreadEntry(void *func) {

    while(!stop){
        std::cout << "going!" << std::endl;
        sleep(1);
    }
}
//---------------------------------------------------------

db_c_hndlr::~db_c_hndlr() {
    int i = 0;
    std::vector<db_transact*>::iterator it;
    for (i=0, it = _db_pool.begin();it!=_db_pool.end();it++, i++) {
        if (_db_pool[i])
            if (_db_pool[i]!=NULL) 
                delete _db_pool[i];
    }
}
//---------------------------------------------------------

db_transact *db_c_hndlr::db_conn_get(void) {
    db_transact *tmp;

    tmp = new db_transact(_global->get_child("db_config"), _ns, _log_id);
    _db_pool.push_back(tmp);
    return tmp;
}
//---------------------------------------------------------
int main(void)
{
    db_transact *conn=NULL;
    db_c_hndlr db;
    //db = new db_c_hndlr();

    conn= db.db_conn_get();
    return OK;
}

}