|
-
August 6th, 2009, 10:09 AM
#1
LoadLibrary and string issue
Hi to all
I am making a software with Visual Studio 2008, writing in C++ (no MFC).
I would like to load some DLL dynamically.
Following msdn example (ms-help://MS.MSDNQTR.v90.it/dllproc/base/using_run_time_dynamic_linking.htm), I would like to create a funcion address like this:
funct_addr = (MYPROC) GetProcAddress(hinstDLL, "myDLLFunct");
and subsequently calling this function like this:
(funct_addr) (L"Message sent to the DLL function\n");
My question is: how could I call a funcion which argouments are made by strings?
In my code, I have the following function:
myDLLFunct("string1","string2");
How could I call it? I can't do something like this:
(funct_addr) (L""string1","string2"");
Thank's for your help!
-
August 6th, 2009, 12:15 PM
#2
Re: LoadLibrary and string issue
I'm not sure if I got your question right, but from what I understand (and if one removes obvious typos, or errors/VB left-overs, like L""string1","string2""), every API function that has a string in one of its arguments, imported dynamically through the call to GetProcAddress() must be specifically identified by its ANSI/Unicode name. Here's an example:
Code:
HANDLE hMod = LoadLibrary(_T("User32.dll"));
#ifdef _UNICODE
(FARPROC&)fnPtr = GetProcessAddress(hMod, _T("OpenDesktopW"));
#else
(FARPROC&)fnPtr = GetProcessAddress(hMod, _T("OpenDesktopA"));
#end
if(fnPtr)
fnPtr(_T("Screen-saver"), 0, FALSE, NULL);
You'd also better use _T("") macro on all of your strings to facilitate ANSI/Unicode conversion.
-
August 6th, 2009, 01:29 PM
#3
Re: LoadLibrary and string issue
 Originally Posted by motobizio
In my code, I have the following function:
myDLLFunct("string1","string2");
How could I call it? I can't do something like this:
(funct_addr) (L""string1","string2"");
You call the function using the parameters that it requires.
If this is the prototype of the function:
void myDLLFunct(const char *, const char *);
Then you call it using a pointer obtained from GetProcAddress() like this:
funct_addr("string 1", "string 2");
And as dc_2000 noted, be careful when using _T() MACROS. Once a dll has been compiled you must know what type of strings it requires so _T() may not be appropriate. Microsoft usually provides two versions 'A' and 'W' as indicated above.
-
August 6th, 2009, 01:37 PM
#4
Re: LoadLibrary and string issue
You can always #ifdef _UNICODE your function prototypes (providing both A and W versions are available) so that you can be consistent with the Win32 api's and continue to use LPTSTR and _T("").
-
August 7th, 2009, 02:34 AM
#5
Re: LoadLibrary and string issue
First of all Thank you for your help.
For dc_2000:
I tried to do something like this:
funct_addr(_T"("string1","string2")");
but it returns following error:
error C2197: too many argouments for a calling
For 0xC0000005:
I tried to do something like this:
funct_addr(L""string1","string2"");
but it returns following error:
error C2146: syntacs error: ')' missing before string1 argoument
For Arjay:
My project is in UNICODE: I put UNICODE directive in project property
Let me explain well.
I have 2 functions from the same DLL:
myDLLFunct1(int arg1, int arg2);
myDLLFunct2(TCHAR* pstring1, TCHAR* pstring2)
I would like to assign an address for those 2 functions by this way:
funct1_addr = (MYPROC) GetProcAddress(hinstDLL, "myDLLFunct1");
funct2_addr = (MYPROC) GetProcAddress(hinstDLL, "myDLLFunct2");
and subsequently calling this function like this:
funct1_addr (L"arg1, arg2");
funct2_addr (L""string1","string2"");
Well, funct1_addr works OK, while funct2_addr doesn't work because of errors during compile action.
Does anyone know how could I pass those strings as argouments?
Thank you again!
-
August 7th, 2009, 02:51 AM
#6
Re: LoadLibrary and string issue
 Originally Posted by motobizio
and subsequently calling this function like this:
funct1_addr (L"arg1, arg2");
funct2_addr (L""string1","string2"");
Why are you enclosing the parameters in quotes? That is not needed! The function calls should look as follows:
Code:
funct1_addr (arg1, arg2);
funct2_addr ("string1","string2");
-
August 7th, 2009, 03:37 AM
#7
Re: LoadLibrary and string issue
 Originally Posted by kolkoo
Why are you enclosing the parameters in quotes? That is not needed! The function calls should look as follows:
Code:
funct1_addr (arg1, arg2);
funct2_addr ("string1","string2");
Because if I write as you suggest:
funct1_addr(arg1, arg2);
the compiler returns following error:
error C2197: too many argouments for a calling
I defined my funct_addr type like this (I found it in a mdsn example):
Code:
typedef int (__cdecl *MYPROC)(LPWSTR);
MYPROC funct1_addr ;
-
August 7th, 2009, 04:19 AM
#8
Re: LoadLibrary and string issue
then you should call it this way:
Code:
funct1_addr("String parameter here!");
-
August 7th, 2009, 04:59 AM
#9
Re: LoadLibrary and string issue
Well I partially solved it.
The problem was in the function type of funct_addr.
I declared it like mdsn example by this way:
typedef int (__cdecl *MYPROC)(LPWSTR);
MYPROC funct1_addr ;
Now, declaring as (FARPROC&) (Thank's a lot dc_2000) the compilation results OK!!!
Thanks to all of you!!
But now I have another issue.
Actually I link my dll in implicit way, by passing MyDll.lib in the linker section of project property sheet.
Because of I would like to charge it dinamically, I avoid the link to DLL in the linker section, and insert it in my code with Loadlibrary() function. This fact cause a no good compilation because linker can't find funct1_addr.
Where I fault this time?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|