Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
---------------------structure in dll

C#
#pragma pack(1)
    struct __declspec(dllexport)  summary
    {
        char DeviceName[300];
        char DeviceId[15];
        char DeviceClass[10];
        char DeviceComport[6];   
        unsigned long long DevicelongId;
        int DeviceCount;
    }Summary[100];


-------------------------in console application

in .h

C#
struct __declspec(dllimport)  summary
{
    char DeviceName[300];
    char DeviceId[15]; 
    char DeviceClass[10];
    char DeviceComport[6];   
    unsigned long long DevicelongId;
    int DeviceCount;
}Summary[100];


in .c file

C++
    HINSTANCE  hMod = LoadLibrary (L"BTCdll.dll");
if (NULL == hMod)
    printf ("LoadLibrary failed\n");
else
    printf ("LoadLibrary LOADED\n");

FARPROC initfn = GetProcAddress(hMod,  (LPCSTR)MAKEWORD(4,0));

    initfn();// some init function which loads correctly

    //i want structure like
    printf("%d",Summary[1]->DeviceCount);


How to import my structure in the console application code ?


[edit originator="01.mandar"]
dll export details

==================================================
Function Name     : public: struct summary & __thiscall summary::operator=(struct summary const &)
Address           : 0x1002708c
Relative Address  : 0x0002708c
Ordinal           : 2 (0x2)
Filename          : BTCdll.dll
Full Path         : D:\BTCdll.dll
Type              : Exported Function        <<<=====(this is struct summary..!)
==================================================

==================================================
Function Name     : int __cdecl BT_Initialise2(void)
Address           : 0x10027d7f
Relative Address  : 0x00027d7f
Ordinal           : 3 (0x3)
Filename          : BTCdll.dll
Full Path         : D:\BTCdll.dll
Type              : Exported Function
==================================================

i have made the BTCdll.dll and exported 2 struct and 2 function. I can LoadLibrary and call 2 function successfully.
when the function init is executed it will populate the 2 struct which i want to import in console application.

can I
----in BTCdll.h
include the struct like

C++
struct __declspec(dllimport)  summary
    {
        char DeviceName[300];
        char DeviceId[15];
        char DeviceClass[10];
        char DeviceComport[6];   
        unsigned long long DevicelongId;
        int DeviceCount;
    }Summary[100];

can i some how get "Summary[100]" which is populated

--- in console.c

C++
FARPROC mystruct= GetProcAddress(hMod,  (LPCSTR)MAKEWORD(2,0));

int cnt=mystruct[3]->DeviceCount;


[/edit]
Posted
Updated 18-Oct-11 22:25pm
v2

1 solution

You need the ordinal or name of the Summary structure which you must get, again, by GetProcAddress(). If this is not your own dll (I assume not) then either use the documentation or Dependency Walker[^] to get the relevant details.
 
Share this answer
 
Comments
01.mandar 19-Oct-11 2:33am    
Comment moved to original question.
Richard MacCutchan 19-Oct-11 4:29am    
It appears that you can access the structure by getting the address of ordinal item 2 of the DLL. However, if this is a DLL that you created, and you have the source code, it would be a better implementation to move the structure into your console application and just send its address to the initialise function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900