473,473 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

LPCWSTR error i have in my C code

29 New Member
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
12 11631
gpraghuram
1,275 Recognized Expert Top Contributor
Hi,
I think this will help you.
Thanks
Raghu
Dec 22 '09 #2
solita
17 New Member
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
Amera
29 New Member
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
9,208 Recognized Expert Moderator Expert
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
Amera
29 New Member
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
9,208 Recognized Expert Moderator Expert
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
Amera
29 New Member
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
9,208 Recognized Expert Moderator Expert
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
Amera
29 New Member
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
Amera
29 New Member
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
Amera
29 New Member
ok , i know it means null pointer .

but how can i fix it ??
Dec 23 '09 #12
Amera
29 New Member
thank you very much for your great help my friends.

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

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

Similar topics

3
by: Michael Breidenstein | last post by:
Hi, anybody can help me to convert a "char *" to LPCWSTR with the following function: LPCWSTR charP2LPCWSTR(char *szText){ LPCWSTR tmp; tmp=szText; // At this point I need help
5
by: Paul | last post by:
Hi, Any one knows how to convert CString to LPCWSTR? Please advice, thanks! -P
3
by: Maileen | last post by:
Hi, How can we convert string^ to String or to LPCWSTR ? thx, Maileen
9
by: ka1cqd | last post by:
I have been looking all over the place for a method to take command line arguments and convert them to a string or wstring so i can process the data and then covert the resulting strings to...
2
by: Neo | last post by:
how convert LPCSTR to LPCWSTR? regards, Mohammad Omer Nasir.
1
by: asenthil | last post by:
Hai this is senthil... I had tried to write a string which fetched from a database. into a file... when i tried to compile the solution the following error occurs like this error C2664:...
2
by: dehi | last post by:
Hello, My problem ist that I don't know how to convert a variable from LPWSTR to LPCWSTR. Anyone an idea? Regards, Denis
5
by: sachin30 | last post by:
How do i convert const char * to LPCWSTR?
9
by: X Enterprises | last post by:
Hello :) I'm not new to C++, I just don't use it as much as I used to. But anyway, I need to convert LPSTR to LPCWSTR. Here's the function i'm using it in: MYCOMMAND void PrintText( LPSTR...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.