Jump to content
  • Advertisement
Sign in to follow this  
rohini chandra

Using GetProcAddress in C

This topic is 4204 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am using VC++ 7.1 I need to call a function from a dll(MSQ.dll) using run time loading from the application app.exe I would be thankful if anyone can see the below piece of code and see if there are any mistakes in the way the function ModuleStateQuery() is called. The function ModuleStateQuery() is exported using a .def file.I have checked the dll exports using dumpbin and the function name is not decorated. I am not getting the correct return value that is expected. ==========MSQ.dll(C code compiled in VC++ 7.1)========= extern "C" int FAR PASCAL ModuleStateQuery(const char * moduleName){ } =============================================== =====app.exe(C code compiled in VC++ 7.1)============== HMODULE hDLL_ = NULL; FARPROC ModuleStateQuery; int active; hDLL_ = LoadLibrary("MSQ.dll"); if(hDLL_) { ModuleStateQuery = GetProcAddress(hDLL_,"ModuleStateQuery"); if (ModuleStateQuery != NULL) { active = (ModuleStateQuery)("MODULEA\0"); } } ================================================ Thanks in advance, Rohini Chandra

Share this post


Link to post
Share on other sites
Advertisement
Declare a typedef for the function pointer and use that instead of FARPROC.



typedef int (FAR PASCAL *ModuleStateQueryT)(const char * moduleName);

HMODULE hDLL_ = NULL;
ModuleStateQueryT ModuleStateQuery;
int active;

hDLL_ = LoadLibrary("MSQ.dll");
if(hDLL_)
{
ModuleStateQuery = (ModuleStateQueryT)GetProcAddress(hDLL_,"ModuleStateQuery");
if (ModuleStateQuery != NULL) {
active = (*ModuleStateQuery)("MODULEA\0");
}



Share this post


Link to post
Share on other sites
What return value are you getting? If it's nonzero, then it must be the address of the function by that name, by definition of GetProcAddress.

Edit: I see now that you meant the return function of ModuleStateQuery, not GetProcAddress. I second the typedef suggestion.

Admiral

Share this post


Link to post
Share on other sites
I would create a typedef (and not use FARPROC). You're probably using a C++ compiler, and FARPROC is a fake type that does not match the declaration of your functions - that would explain your problem.

// in app.exe
typedef int (FAR PASCAL *module_state_query_pfnt)(const char*);

module_state_query_pfnt ModuleStateQuery;
ModuleStateQuery = (module_state_query_pfnt)(GetProcAddress(hDLL_,"ModuleStateQuery"));
if (ModuleStateQuery)
{
// you don't need to zero-terminate your sring - this is done
// automagically by the compiler.
int active = ModuleStateQuery("MODULEA");
}


HTH,

[Damn. beaten]

Share this post


Link to post
Share on other sites
Hi,

Thank you every one.
It works now and I am getting the proper return code that was expected.
Initially I used a function pointer defined by me instead of FARPROC but did not
use the calling convention FAR PASCAL(Is FAR also part of calling convention)
while defining the function pointer(I haven't written the MSQ.dll that the app.exe used.)and so I turned to FARPROC.

Thanking you once again,
Rohini Chandra

Share this post


Link to post
Share on other sites
The FAR statement is a leftover from the days of DOS and 64kb memory segments. The preprocessor replaces it with an empty string.

Share this post


Link to post
Share on other sites
i've a situation where i want to call a dll(i create this dll) and this function return 2 string values.how to get those return value for me to use it im my exe??

here's some of my codes:
//declaration
typedef int (*MYPROC)(LPTSTR);
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;

char libname[100];
char funcname[100];
char tmpbuf[1000];

// Get a handle to the DLL module.
hinstLib = LoadLibrary(libname);

// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, funcname);


// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
(ProcAdd) (tmpbuf); //here's i send the input value//
//but where should i get the return value??//
}

// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}

// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
printf("Message via alternative method\n");

Share this post


Link to post
Share on other sites
Quote:
Original post by biggurlz
i've a situation where i want to call a dll(i create this dll) and this function return 2 string values.how to get those return value for me to use it im my exe??


Erm...how does your function manage to return 2 string values when a function can return only one value.

Share this post


Link to post
Share on other sites
Pass two buffers to the function as "out" parameters.


int myfunk(char **buff1, char **buff2)
{
*buff1 = // fill me up
*buff2 = // fill me up
return 0; // return ok or whatever
}

char buffer1[256];
char buffer2[256];

int rok = myfunk(&buffer1, &buffer2);

printf("%s, %s", buffer1, buffer2);



This doesn't seem to me like a question of how to use loadlibrary/getprocaddress.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!