CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Oct 2005
    Location
    Chennai, India
    Posts
    27

    Arrow Run-Time Check Failure #0

    Hello friends,

    I created a simple dll. The code in the dll is :
    Code:
    extern "C" __declspec(dllexport)
    int fact(int n)
    {
    	int i, fac;
    	for(i = 2, fac = 1; i <= n; i++)
    		fac*=i;
    	return fac;
    }
    And i called the dll from this program:
    Code:
    #include <windows.h>
     #include <stdio.h>
    
     typedef int (WINAPI*function)(int);
    
     function fact;
    
     int main()
     {
    	   int i=0;	  
    
    	   HINSTANCE hLib=LoadLibrary("fact.DLL");
    
    
    	   if(hLib==NULL) {
    
    			printf("Unable to load library!");
    			getchar();
    			return 0;
    	   }
    
    	   char mod[50];
    
    	   GetModuleFileName((HMODULE)hLib, (LPTSTR)mod, 50);
    	   printf( "Library loaded: %s\n", mod );
    
    
    	   fact=(function)GetProcAddress((HMODULE)hLib, "fact");
    	   
    	   if(fact==NULL)
    	   {
    			printf("Unable to load function fact...\n");
    			FreeLibrary((HMODULE)hLib);
    			getchar();
    			return 0;
    	   }
    		
    	   i = fact(5);								//call the function.
    	   printf("The factorial of 5 is %d",i);  //should display 120.
    
    
    	   FreeLibrary((HMODULE)hLib);
    	   getchar();
     }
    When i run this program, this error message comes up:
    Code:
    Run-Time Check Failure #0 - The value of ESP was not properly 
    saved across a function call.
    This came in the line:
    Code:
     i = fact(5);
    I dont know how to solve this one.
    But the factorial is calculated correctly and displayed in the window as 120!

    Please help me in solving this problem.
    Thank You,
    Paramesh.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Run-Time Check Failure #0

    You declared the DLL with a different calling convention than the function pointer. In C++, you have __cdecl and __stdcall calling conventions. You must declare your function pointer to match the proper calling convention as your DLL function.

    WINAPI I believe is __stdcall, but your DLL function is a __cdecl function. Make sure that your DLL and app both declare the DLL function as __cdecl or __stdcall (you have a choice in which one to fix). If you were to fix the DLL, you would do this:
    Code:
    extern "C" __declspec( dllexport ) __stdcall
    int fact(int);
    Then your declaration of WINAPI for the function pointer in your app would match your DLL function's calling convention (in this case __stdcall). If you didn't want to change your DLL, then your app should declare a __cdecl function pointer (in this case, you don't need the WINAPI -- just declare an "ordinary" function pointer (no WINAPI), since ordinary C and C++ functions are automatically __cdecl).

    The calling convention determines how the parameters are passed to the function (which parameter actually goes first on the stack), and which function is responsible for cleaning up the stack. A mismatch in calling convention causes the error you are seeing.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Oct 2005
    Location
    Chennai, India
    Posts
    27

    Re: Run-Time Check Failure #0

    Paul. Thank you for your replies.

    But your suggestion gave me two errors:
    Code:
    fact.cpp(3) : warning C4518: 'int ' : storage-class or type specifier(s) unexpected here; ignored
    fact.cpp(3) : warning C4230: anachronism used : modifiers/qualifiers interspersed; qualifier ignored
    And when i tried to run the program, it said that unable to load the function fact.

    What should i do now?
    Thanks.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Run-Time Check Failure #0

    Code:
    extern "C" int __declspec(dllexport) __stdcall
    fact(int n)
    {
    	int i, fac;
    	for(i = 2, fac = 1; i <= n; i++)
    		fac*=i;
    	return fac;
    }
    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Oct 2005
    Location
    Chennai, India
    Posts
    27

    Re: Run-Time Check Failure #0

    Thank You for your reply paul.

    In that case, how would i call the dll?
    When i do like this:
    Code:
     typedef int (WINAPI*function)(int);
    
     function fact;
    It says that unable to load function fact.


    Paramesh.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Run-Time Check Failure #0

    What is the name of the exported DLL function? Not the name you think it is, but the actual name? To see what the name is, use the Dependency Walker utility (depends.exe) and look at the exported names.

    The name is probably not "func", but it is "func@4" or something similar to that. When you call GetProcAddress(), you must give the exact name of the function.

    To alleviate this, you can create a DEF file for the DLL, so that the "@4" is stripped from the final exported name. Please see the FAQ:

    http://www.codeguru.com/forum/showthread.php?t=231254

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Oct 2005
    Location
    Chennai, India
    Posts
    27

    Re: Run-Time Check Failure #0

    I already have the def file created!

    But it still gives me the runtime exception.
    When i tried to run the same program in Dev C++, it gave me no error.(irrelevant to the forum!)

    Can you give me a sample visual C++ solution?
    Thank You.
    Paramesh.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Run-Time Check Failure #0

    Quote Originally Posted by cegparamesh
    I already have the def file created!
    So what happened? Did you use it correctly? Did you make it part of your project?
    But it still gives me the runtime exception.
    Now you are changing your story. You first said that the message was printed, now you say that it caused a runtime exception. It is difficult to follow what your current situation is.

    What is the name of the exported function? You never verified the actual name of the function that is exported from the DLL.

    Can you give me a sample visual C++ solution?
    I did that already, but you are doing something wrong.

    First, please verify what the name of the function is in the DLL. Second, is it a runtime error, does the LoadLibrary() fail, does the GetProcAddress() fail, etc. etc.?

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Oct 2005
    Location
    Chennai, India
    Posts
    27

    Re: Run-Time Check Failure #0

    Quote Originally Posted by Paul McKenzie
    So what happened? Did you use it correctly? Did you make it part of your project?
    Now you are changing your story. You first said that the message was printed, now you say that it caused a runtime exception. It is difficult to follow what your current situation is.

    What is the name of the exported function? You never verified the actual name of the function that is exported from the DLL.

    I did that already, but you are doing something wrong.

    First, please verify what the name of the function is in the DLL. Second, is it a runtime error, does the LoadLibrary() fail, does the GetProcAddress() fail, etc. etc.?

    Regards,

    Paul McKenzie
    Paul, You are a great helper!
    I thought you would just ignore my request.

    Anyway. Here is my total thingy:

    DLL:
    fact.cpp:
    Code:
    extern "C" int __declspec(dllexport) __stdcall
    fact(int n)
    {
    	int i, fac;
    	for(i = 2, fac = 1; i <= n; i++)
    		fac*=i;
    	return fac;
    }
    fact.def:
    Code:
    LIBRARY fact
    DESCRIPTION 'This is my DLL
    
    EXPORTS
    fact
    This compiled fine and i saved a copy of the fact.dll in the system32 folder.

    Here is the Calling program:
    main.cpp:
    Code:
    #define WIN32_LEAN_AND_MEAN	 
    #include <windows.h>
     #include <stdio.h>
    
    typedef int (WINAPI*function)(int);
    function fact;
    
     int main()
     {
    	   int i=0;	  
           char mod[50];
    
           HINSTANCE hLib=LoadLibrary("fact.dll");
    
    
           if(hLib==NULL) {
    
                printf("Unable to load library!");
                getchar();
                goto end;
           }
    
           GetModuleFileName((HMODULE)hLib, (LPTSTR)mod, 50);
           printf( "Library loaded: %s\n", mod );
    
    
           fact=(function)GetProcAddress((HMODULE)hLib, "fact");
           
           if(fact==NULL)
    	   {
                printf("Unable to load function fact...\n");
                FreeLibrary((HMODULE)hLib);
    			goto end;
           }
    		
    	   i = fact(5);
    	   printf("The factorial of 5 is %d",i);
    
           FreeLibrary((HMODULE)hLib);
    end:
    	   getchar();
     }
    Here is the output:
    Code:
    Library loaded: C:\WINDOWS\system32\fact.dll
    Unable to load function fact...
    Can you find where is the error?
    Thanks.
    Paramesh.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Run-Time Check Failure #0

    1) You still didn't verify that the function in the DLL is actually called "fact". Please use the dumpbin or depends.exe (Dependency Walker) program that is part of the VC++ tools to verify the name of the exported function in the DLL. Do you or have you ever used those tools? You can't go any further until you tell me what the name of the function really is.

    I cannot tell by what you posted what is wrong. It doen't matter if it compiled or made a DLL -- the most important part is the name of exported function, which you have left out. The reason why GetProcAddress() returns NULL is that the function is not called "fact". Trust me, it isn't called that -- that's why you need to use the tools I mentioned above to know what the name of the exported function is.

    2) The DEF files that I use always have an export ordinal number. Look at the examples in the FAQ.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Oct 2005
    Location
    Chennai, India
    Posts
    27

    Re: Run-Time Check Failure #0

    Paul, Thank you.

    I downloaded the depends.exe and found that the name of the function is not fact.
    it is: _fact@4

    Now, the program runs fine.

    But still, i have one question:
    Why didnt the function name change after using the EXPORTS fact statement in the .def file?

    Even i tried EXPORTS fact @4, but still, the function name is _fact@4.


    Paramesh.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Run-Time Check Failure #0

    Quote Originally Posted by cegparamesh
    Paul, Thank you.

    I downloaded the depends.exe and found that the name of the function is not fact.
    it is: _fact@4

    Now, the program runs fine.

    But still, i have one question:
    Why didnt the function name change after using the EXPORTS fact statement in the .def file?

    Even i tried EXPORTS fact @4, but still, the function name is _fact@4.


    Paramesh.
    That's why I asked you if the DEF file is part of your project. If it is part of the project, then the ordinal value in the DLL will match the ordinal number you specfied in the DEF file. Did you add the DEF file to your project workspace?

    Rebuild your DLL, and use a different ordinal number in your DEF file (use @20 or something like that. Now does your DLL funciton "fact" have an ordinal of @20 when you use depends.exe? If not, this means that your DEF file is not being recognized when you built your DLL.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Oct 2005
    Location
    Chennai, India
    Posts
    27

    Re: Run-Time Check Failure #0

    Paul,

    I have added the DEF file to the project workspace.
    I changed the EXPORTS fact to all possible values. No use. The name is still _fact@4.

    It seems that the DEF file is not recognized when i build the DLL.
    What should i do now?

    Thanks,
    Paramesh.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Run-Time Check Failure #0

    Quote Originally Posted by cegparamesh
    Paul,

    I have added the DEF file to the project workspace.
    I changed the EXPORTS fact to all possible values. No use. The name is still _fact@4.

    It seems that the DEF file is not recognized when i build the DLL.
    What should i do now?

    Thanks,
    Paramesh.
    Did you do a clean rebuild of the DLL?

    Are you sure you are using the right DLL (are you using the one you just created, or are you mistakingly using an old version)?

    What is the ordinal number when you view the DLL using depends.exe? Make sure you restart depends.exe each time you create the DLL, since I've experienced depends.exe retaining old information if you don't restart it.

    The DEF file should have at lease one space between the function name and the ordinal.
    Code:
    EXPORTS
    fact    @20
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 27th, 2005 at 10:18 AM.

  15. #15
    Join Date
    Oct 2005
    Location
    Chennai, India
    Posts
    27

    Re: Run-Time Check Failure #0

    Paul,

    I rebuilded and found no change.
    I am testing the right dll.
    The ordinal number it showed is 0x0001.
    and i left a space between the function name and ordinal.

    I dont know how stupid i am. I cant even do a simple DLL.
    Thank you very much for you help.

    Paramesh.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured