By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,938 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,938 IT Pros & Developers. It's quick & easy.

LPCWSTR error i have in my C code

P: 29
hi,


i wrote a c code which calls a function from mytest,dll
this function returns the character "s" .

this is my code :

#include<stdio.h>
#include<windows.h>

int main(){

HANDLE ldll;
char (*str)(void);


ldll = LoadLibrary("mytest.dll");

str = GetProcAddress(ldll, "str");

printf("result : %c", str);

}

and i get this errors :

warning C4133: 'function' : incompatible types - from 'char [11]' to 'LPCWSTR'

warning C4047: 'function' : 'LPCSTR' differs in levels of indirection from 'char (__cdecl *)(void)'

warning C4024: 'GetProcAddress' : different types for formal and actual parameter 2

error C2440: '=' : cannot convert from 'FARPROC' to 'char (__cdecl *)(void)'


please help me ,

thx in advanced.
Dec 22 '09 #1

✓ answered by solita

Try the following.Perform the following casting and see if it works.You should cast the farproc to char (__cdecl *)(void).

Hope it helps.

#include<stdio.h>
#include<windows.h>

int main(){

HANDLE ldll;
typedef char (*str)(void);
str Str;

ldll = LoadLibrary("mytest.dll");

Str = (str)GetProcAddress(ldll, "str");

printf("result : %c", str);

}

Share this Question
Share on Google+
12 Replies


gpraghuram
Expert 100+
P: 1,275
Hi,
I think this will help you.
Thanks
Raghu
Dec 22 '09 #2

P: 17
Try the following.Perform the following casting and see if it works.You should cast the farproc to char (__cdecl *)(void).

Hope it helps.

#include<stdio.h>
#include<windows.h>

int main(){

HANDLE ldll;
typedef char (*str)(void);
str Str;

ldll = LoadLibrary("mytest.dll");

Str = (str)GetProcAddress(ldll, "str");

printf("result : %c", str);

}
Dec 22 '09 #3

P: 29
ok ,now i can see the console with this written on :
result : (i couldn't see it before, no console was shown)

but my character is not there .I have done what you told me but i get this error in my dll :

error C2059: syntax error : ')'
error C2143: syntax error : missing ';' before '{'
error C2447: '{' : missing function header (old-style formal list?)

the error is in mytest.cpp code in the function deceleration line :
EXPORT char (__cdecl *)(void)

this is my code :





#include "mytest.h"

EXPORT char (__cdecl *)(void){

char x='s';
return x;
}

BOOL WINAPI DllMain(HANDLE hModule,DWORD dwReason,LPVOID lpReserved){
return TRUE;
}
Dec 22 '09 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
Don't forget that str is function pointer. This is wrong:

Expand|Select|Wrap|Line Numbers
  1. printf("result : %c", str);
You need to actually call the function:

Expand|Select|Wrap|Line Numbers
  1. printf("result : %c", str());
Dec 22 '09 #5

P: 29
yes , you are right but i still have error in passing my character :

the error in my dll :

error C2059: syntax error : ')'
error C2143: syntax error : missing ';' before '{'
error C2447: '{' : missing function header (old-style formal list?)


and this error in my caller code :

warning C4133: 'function' : incompatible types - from 'char [11]' to 'LPCWSTR'

how can i solve this issue ??
Dec 22 '09 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
What is this:

Expand|Select|Wrap|Line Numbers
  1. EXPORT char (__cdecl *)(void){
  2.  
  3. char x='s';
  4. return x;
  5. }
?

If this is a function where is the function name and where is the char return?

Like maybe:

Expand|Select|Wrap|Line Numbers
  1. __dllexport char WINAPI str()
  2. {
  3.  
  4. char x='s';
  5. return x;
  6. }
Dec 22 '09 #7

P: 29
i did what you said but i still have the same problem .

you are right , there should be the function name here :

EXPORT char (__cdecl *)(void){

i want to use a poniter to the function in my dll because i'm using it in my C code :
typedef char (*str)(void);
so i can get my character .

i'm getting a null pointer when i use C interpreter and when i'm using VS 2005
i get this error : incompatible types - from 'char [11]' to 'LPCWSTR'

so i'm still have the same problem.
Dec 22 '09 #8

weaknessforcats
Expert Mod 5K+
P: 9,197
The DLL has the function. I looks like any other function.

GetProcAddress returns the address of that function.

There must be a function pointer variable fore the correct number and type of arguments and the correct return type.

You now typecast the FARPROC returned by GetProcAddress to a pointe to a function of the correct number and type of arguments and correct return type. You assign this typecast to your function pointer, then use th function pointer to make the call.

There is no pointer in the DLL.


The LPCWSTR problem is due to "mytest.dll", which is a const array of char. That is, a LPCSTR. You will have to convert the LPCSTR to an LPWSTR to make the LoadLibrary call.

Ditto for the "str" in GetProcAddress.

Thisis caused by the Unicode character set property setting in your project.

I recommend that you start using TCHAR so you don't have to worry about this OR just code everything in Unicode.

The TCHAR macro for a literal is TEXT:

Expand|Select|Wrap|Line Numbers
  1. ldll = LoadLibrary(TEXT("mytest.dll"));
Dec 22 '09 #9

P: 29
i did this :


mytest.h

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. #ifndef __DLL_H_
  5. #define __DLL_H_
  6.  
  7. #include <windows.h>
  8.  
  9.  
  10. #define EXPORT __declspec(dllexport)
  11. #else
  12. #define EXPORT __declspec(dllimport)
  13. #endif
  14.  
  15. #ifdef __cplusplus
  16. extern "C"
  17. {
  18. #endif
  19.  
  20. int EXPORT __stdcall str(char f);
  21.  
  22. #ifdef __cplusplus
  23.  
  24. }
  25.  
  26. #endif
  27.  
  28.  
  29.  


mytest.cpp

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include "mytest.h"
  3.  
  4. int EXPORT __stdcall str(char f){
  5.   return 1;
  6. }
  7.  
  8.  
  9. BOOL WINAPI DllMain(HANDLE hModule,DWORD dwReason,LPVOID lpReserved){
  10.         return TRUE;
  11.  
  12.     }
  13.  
  14.  

rundll.c

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include<stdio.h>
  4. #include<windows.h>
  5.  
  6. main(){
  7.  
  8. HANDLE ldll;
  9.  
  10. typedef int (*str)(char f);
  11.  
  12. str Str;
  13.  
  14. ldll = LoadLibrary(TEXT("mytest.dll"));
  15.  
  16. Str = (str)GetProcAddress(ldll, "str");
  17.  
  18. char k = 's';
  19.  
  20. printf("result : %d", Str(k));
  21.  
  22.  
  23. }
  24.  
  25.  

and i get this errors from runndll.c :


Error 1 error C2143: syntax error : missing ';' before 'type'

Error 2 error C2065: 'k' : undeclared identifier
Dec 23 '09 #10

P: 29
Thank you so much for your great help .

i don't have any errors anymore , just one thing !!

when the function returns an integer , ex : 1

it is written " result : 1" in the console ...

but when it returns a character , the character is shown as a smiley face !!
Dec 23 '09 #11

P: 29
ok , i know it means null pointer .

but how can i fix it ??
Dec 23 '09 #12

P: 29
thank you very much for your great help my friends.

everything is working fine now .
Dec 23 '09 #13

Post your reply

Sign in to post your reply or Sign up for a free account.