在程序中定义了一个结构体,如何将结构体传入动态连接库(Dll),并返回结构体值。

解决方案 »

  1.   

    http://topic.csdn.net/t/20021206/12/1237118.html
      

  2.   

    和普通的类一样使用,结构体可以看作是一个全部为public的class
      

  3.   

    这个应该没什么问题
    你在DLL中定义相同结构
    把new出的结构的指针p传入DLL接口,DLL通过:
    YourStruct* pStruct = reinterpret_cast<YourStruct*>(p);
    这样就可以了.
    不知道你是否是这个意思?
      

  4.   

    比如我定义了一个结构体
    typedef struct{
    int x1;
    int x2;
    int x3;
    int x4;
    }test;
    然后我想把对此结构体的赋值写在dll中,我就是不知到如何传到dll中,如何返回!
      

  5.   

    int Read(test *ptest);
    { // ptest 值改变并返回之}int Write(test *ptest);
    { //同上}
      

  6.   

    我按照楼上的做了,出现如下错误,不知是怎么一会事?
    --------------------Configuration: InsSys - Win32 Debug--------------------
    Compiling...
    InsSys.cpp
    Generating Code...
    Compiling...
    InsDBDate.cpp
    Generating Code...
    Linking...
    InsSys.obj : error LNK2001: unresolved external symbol "public: int __thiscall DBData::read(int,struct test *)" (?read@DBData@@QAEHHPAUtest@@@Z)
    InsSys.obj : error LNK2001: unresolved external symbol "struct test *  ptest" (?ptest@@3PAUtest@@A)
    Debug/InsSys.exe : fatal error LNK1120: 2 unresolved externals
    Error executing link.exe.
    Creating browse info file...InsSys.exe - 3 error(s), 0 warning(s)
      

  7.   

    我在dll中写了这样一个函数
    EXPORT bool WRITE(extern test *ptest)
    {
    memset(&ptest, 0x00, sizeof(ptest));
    ptest[0].x1=2+1;
    ptest[0].x2=2+2;
    ptest[0].x3=2+3;
    ptest[0].x4=2+4;
    return true;}
    在exe中写一个函数调用
    int read(test *ptest)
    {
    int a;
    a=0;
    typedef bool (_cdecl *TESTDLL)(test *ptest);
    HINSTANCE hmod;
    TESTDLL lpproc;
    hmod = ::LoadLibrary ("GetCell.dll");
    if(hmod==NULL)
    {
    return 0;
    }
    lpproc = (TESTDLL)::GetProcAddress(hmod,"WRITE");
    if(lpproc)
    {
    a=ptest[0].x1;
    a=ptest[0].x2;
    a=ptest[0].x3;
    a=ptest[0].x4;
    }
    else
    {
    return 0;
    }
    FreeLibrary(hmod);
    return 1;
    }
    结果lpproc没有值,这是怎么回事?
      

  8.   

    LoadLibrary()成功了吗?GetProcAddress()后调用GetLastError()看看是什么错误
      

  9.   

    你的传值有问题,应该是test &ptest
      

  10.   

    将这个结构定义在某个头文件中,在exe 和dll中同时include, 可以把结构指针作为dll 函数参数. 但是缺点是结构是定死的, 修改后要重新编译exe 和dll