Thread: An error in my c code

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    10

    An error in my c code

    hello ,

    this is my C code :

    Code:
    #include<stdio.h>
    #include<windows.h>
    
    int 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 these errors :

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

    Error 2 error C2065: 'k' : undeclared identifier

    i couldn't find out what the problem is .

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    You can't declare variables after code. Move the k declaration to above the line where you call LoadLibrary
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    thank you ^^"


    another Q , when the function returns a character , the character is shown in the console as a

    smiley face !!

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    Quote Originally Posted by Amera View Post
    thank you ^^"


    another Q , when the function returns a character , the character is shown in the console as a

    smiley face !!
    Smiley face means the character was the null character (value 0)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    how can i fix this ?

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    Quote Originally Posted by Amera View Post
    how can i fix this ?
    I have no idea, since the value is probably coming from the function in the DLL, and you didn't post the code for that.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    ok , this is my dll :


    mytest.cpp

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


    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
    
    char EXPORT __stdcall str(char f);
    
    
    #ifdef __cplusplus
    
    }
    
    #endif

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    10




    when i change the function returned type into integer it works fine !!



  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    The problem is in mytest.h:

    Code:
    #ifndef __DLL_H_
    #define __DLL_H_
    
    #include <windows.h>
    
    
    // You are missing an #ifdef here, but I don't know what.
    #define EXPORT __declspec(dllexport)
    #else // this else matches with the #ifndef __DLL_H_ above
    // Therefore, none of the following code is seen by the compiler
    #define EXPORT __declspec(dllimport)
    #endif
    
    
    
    #ifdef __cplusplus
    extern "C"
    {
    #endif
    
    char EXPORT __stdcall str(char f);
    
    
    #ifdef __cplusplus
    
    }
    
    #endif
    
    Your function is not being declared. Therefore the compiler assumes that it returns int. Therefore it only works correctly when it returns int.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    Thank you very much my friend ^^

    Everything is working fine now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM