Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C
include <Windows.h>
#include <stdio.h>
#include <string.h>
#ifdef __cplusplus
extern "c" {
    #endif
    __declspec(dllexport)char abc;
    __declspec(dllexport)char hello;
#ifdef __cplusplus
}
#endif

int main (void) {
    HMODULE hMod;
    int *pGlobal,i,pG;
    BOOL fFreeResult;
   
    char d[]="abc";
    char g[]="hello";
 for(i=0;i<5;i++)
 {
   hMod = GetModuleHandle (NULL);
 pGlobal = (int*)GetProcAddress (hMod, d);
        fFreeResult = FreeLibrary(hMod);
        hMod = GetModuleHandle (NULL);
 pG = (int*)GetProcAddress (hMod, g);
    printf("%u\t%u\n",pGlobal,pG);
}
    return 0;
}


This code is working. but i have a question about this line __declspec(dllexport)char abc; in this line char is datatype of variable. if i hvae no idea about datatype of variable then it is possblie. if then how??
Posted
Updated 18-May-14 7:08am
v2
Comments
Sergey Alexandrovich Kryukov 11-Mar-14 3:39am    
If the type is not known, what would you possibly do with such object? :-)
—SA
gupta_abha 11-Mar-14 3:54am    
@Sergey: i want to find address of all global variable,by using getProcAddress(), for this i need
__declspec(dllexport)DataType var_name;but i have name only not type of variable. it is possible way . if then how?
Richard MacCutchan 11-Mar-14 5:13am    
No it is not possible. Variables in memory are just (groups of) bytes, there is nothing to identify their type.
[no name] 11-Mar-14 11:58am    
If the variable name includes C++ name mangling, then you may be able to infer the type. For a symbol exported from C (eg. extern "C") all you know is the name.

1 solution

No. You won't be able to do that. You cannot know the type of a object with only knowing a name. This is hundred percent possible in .NET but not in native C++. When a DLL is compiled there is no class inside it. You can't even know whether it is a 'int' or 'char *'.

How to load a dynamic link library (DLL) into a Microsoft Visual C++ 6.0 project[^]
 
Share this answer
 

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