LPCWSTR error i have in my C code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Amera
    New Member
    • Dec 2009
    • 29

    LPCWSTR error i have in my C code

    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<window s.h>

    int main(){

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


    ldll = LoadLibrary("my test.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.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Hi,
    I think this will help you.
    Thanks
    Raghu

    Comment

    • solita
      New Member
      • Jun 2009
      • 17

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

      Hope it helps.

      #include<stdio. h>
      #include<window s.h>

      int main(){

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

      ldll = LoadLibrary("my test.dll");

      Str = (str)GetProcAdd ress(ldll, "str");

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

      }

      Comment

      • Amera
        New Member
        • Dec 2009
        • 29

        #4
        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;
        }

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Don't forget that str is function pointer. This is wrong:

          Code:
          printf("result : %c", str);
          You need to actually call the function:

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

          Comment

          • Amera
            New Member
            • Dec 2009
            • 29

            #6
            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 ??

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              What is this:

              Code:
              EXPORT char (__cdecl *)(void){
              
              char x='s';
              return x;
              }
              ?

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

              Like maybe:

              Code:
              __dllexport char WINAPI str()
              {
              
              char x='s';
              return x;
              }

              Comment

              • Amera
                New Member
                • Dec 2009
                • 29

                #8
                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.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  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:

                  Code:
                  ldll = LoadLibrary(TEXT("mytest.dll"));

                  Comment

                  • Amera
                    New Member
                    • Dec 2009
                    • 29

                    #10
                    i did this :


                    mytest.h

                    Code:
                    
                    
                    #ifndef __DLL_H_
                    #define __DLL_H_
                    
                    #include <windows.h>
                    
                    
                    #define EXPORT __declspec(dllexport)
                    #else
                    #define EXPORT __declspec(dllimport)
                    #endif
                    
                    #ifdef __cplusplus
                    extern "C"
                    {
                    #endif
                    
                    int EXPORT __stdcall str(char f);
                    
                    #ifdef __cplusplus
                    
                    }
                    
                    #endif


                    mytest.cpp

                    Code:
                    #include "mytest.h"
                    
                    int EXPORT __stdcall str(char f){
                      return 1;
                    }
                    
                    
                    BOOL WINAPI DllMain(HANDLE hModule,DWORD dwReason,LPVOID lpReserved){
                    		return TRUE;
                        
                    	}

                    rundll.c

                    Code:
                    
                    #include<stdio.h>
                    #include<windows.h>
                    
                    main(){
                    
                    HANDLE ldll;
                    
                    typedef int (*str)(char f);
                    
                    str Str;
                    
                    ldll = LoadLibrary(TEXT("mytest.dll"));
                    
                    Str = (str)GetProcAddress(ldll, "str");
                    
                    char k = 's';
                    
                    printf("result : %d", Str(k));
                    
                    
                    }

                    and i get this errors from runndll.c :


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

                    Error 2 error C2065: 'k' : undeclared identifier

                    Comment

                    • Amera
                      New Member
                      • Dec 2009
                      • 29

                      #11
                      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 !!

                      Comment

                      • Amera
                        New Member
                        • Dec 2009
                        • 29

                        #12
                        ok , i know it means null pointer .

                        but how can i fix it ??

                        Comment

                        • Amera
                          New Member
                          • Dec 2009
                          • 29

                          #13
                          thank you very much for your great help my friends.

                          everything is working fine now .

                          Comment

                          Working...