Click here to Skip to main content
15,900,461 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Task Scheduler [modified] Pin
_AnsHUMAN_ 1-Oct-08 1:19
_AnsHUMAN_ 1-Oct-08 1:19 
Questionpoi command in debugger Pin
George_George1-Oct-08 0:55
George_George1-Oct-08 0:55 
QuestionDebug Assertion Failure Pin
bhanu_850930-Sep-08 23:55
bhanu_850930-Sep-08 23:55 
AnswerRe: Debug Assertion Failure Pin
Roger Stoltz1-Oct-08 0:27
Roger Stoltz1-Oct-08 0:27 
GeneralRe: Debug Assertion Failure Pin
bhanu_85092-Oct-08 21:17
bhanu_85092-Oct-08 21:17 
AnswerRe: Debug Assertion Failure Pin
Roger Stoltz2-Oct-08 21:50
Roger Stoltz2-Oct-08 21:50 
GeneralRe: Debug Assertion Failure Pin
bhanu_85094-Oct-08 0:04
bhanu_85094-Oct-08 0:04 
QuestionRun-Time Check Failure Pin
George_George30-Sep-08 23:51
George_George30-Sep-08 23:51 
Hello everyone,


I am writing a C++ client to dynamically load a DLL and call its function, using LoadLibrary and GetProcAddress. In the C++ client, when executing statement -- "if(!StoreData(GetCurrentThreadId()))", after it returned, there is error message, any ideas?

(I showed along with the source codes for C++ client and the DLL itself)

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

DLL Client code:

// The process code

#include <windows.h> 
#include <stdio.h> 
 
#define THREADCOUNT 1 
#define DLL_NAME    TEXT("testdll.dll")

VOID ErrorExit(LPSTR); 

typedef BOOL(*StoreDataFp)(DWORD);
typedef BOOL (*GetDataFp)(DWORD*);
 
DWORD WINAPI ThreadFunc(VOID) 
{   
   int i;

  HMODULE hMod;
  hMod  = LoadLibrary("testdll.dll");

  StoreDataFp StoreData = (StoreDataFp)GetProcAddress(hMod,"StoreData");

  GetDataFp GetData = (GetDataFp)GetProcAddress(hMod, "GetData");

   if(!StoreData(GetCurrentThreadId()))
      ErrorExit("StoreData error");

   for(i=0; i<5; i++)
   {
      DWORD dwOut;
      if(!GetData(&dwOut))
         ErrorExit("GetData error");
      if( dwOut != GetCurrentThreadId())
         printf("thread %d: data is incorrect (%d)\n", GetCurrentThreadId(), dwOut);
      else printf("thread %d: data is correct\n", GetCurrentThreadId());
      Sleep(0);
   }
   return 0; 
} 
 
int main(VOID) 
{ 
   DWORD IDThread; 
   HANDLE hThread[THREADCOUNT]; 
   int i; 
 
// Load the DLL

   LoadLibrary(DLL_NAME);
 
// Create multiple threads. 
 
   for (i = 0; i < THREADCOUNT; i++) 
   { 
      hThread[i] = CreateThread(NULL, // default security attributes 
         0,                           // use default stack size 
         (LPTHREAD_START_ROUTINE) ThreadFunc, // thread function 
         NULL,                    // no thread function argument 
         0,                       // use default creation flags 
         &IDThread);              // returns thread identifier 
 
   // Check the return value for success. 
      if (hThread[i] == NULL) 
         ErrorExit("CreateThread error\n"); 
   } 
 
   WaitForMultipleObjects(THREADCOUNT, hThread, TRUE, INFINITE); 
 
   return 0; 
} 
 
VOID ErrorExit (LPSTR lpszMessage) 
{ 
   fprintf(stderr, "%s\n", lpszMessage); 
   ExitProcess(0); 
}

DLL code:

// The DLL code

#include <windows.h>

static DWORD dwTlsIndex; // address of shared memory
 
// DllMain() is the entry-point function for this DLL. 
 
BOOL WINAPI DllMain(HINSTANCE hinstDLL,  // DLL module handle
    DWORD fdwReason,                     // reason called
    LPVOID lpvReserved)                  // reserved
{ 
    LPVOID lpvData; 
    BOOL fIgnore; 
 
    switch (fdwReason) 
    { 
        // The DLL is loading due to process 
        // initialization or a call to LoadLibrary. 
 
        case DLL_PROCESS_ATTACH: 
 
            // Allocate a TLS index.
 
            if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES) 
                return FALSE; 
 
            // No break: Initialize the index for first thread.
 
        // The attached process creates a new thread. 
 
        case DLL_THREAD_ATTACH: 
 
            // Initialize the TLS index for this thread.
 
            lpvData = (LPVOID) LocalAlloc(LPTR, 256); 
            if (lpvData != NULL) 
                fIgnore = TlsSetValue(dwTlsIndex, lpvData); 
 
            break; 
 
        // The thread of the attached process terminates.
 
        case DLL_THREAD_DETACH: 
 
            // Release the allocated memory for this thread.
 
            lpvData = TlsGetValue(dwTlsIndex); 
            if (lpvData != NULL) 
                LocalFree((HLOCAL) lpvData); 
 
            break; 
 
        // DLL unload due to process termination or FreeLibrary. 
 
        case DLL_PROCESS_DETACH: 
 
            // Release the allocated memory for this thread.
 
            lpvData = TlsGetValue(dwTlsIndex); 
            if (lpvData != NULL) 
                LocalFree((HLOCAL) lpvData); 
 
            // Release the TLS index.
 
            TlsFree(dwTlsIndex); 
            break; 
 
        default: 
            break; 
    } 
 
    return TRUE; 
    UNREFERENCED_PARAMETER(hinstDLL); 
    UNREFERENCED_PARAMETER(lpvReserved); 
}

extern "C" __declspec(dllexport) BOOL __stdcall WINAPI StoreData(DWORD dw)
{
   LPVOID lpvData; 
   DWORD * pData;  // The stored memory pointer 


   // Retrieve a data pointer for the current thread
   lpvData = TlsGetValue(dwTlsIndex); 

   // If NULL, allocate memory for the TLS slot for this thread
   if (lpvData == NULL)
   {
      lpvData = (LPVOID) LocalAlloc(LPTR, 256); 
      if (lpvData == NULL) 
         return FALSE;
      if (!TlsSetValue(dwTlsIndex, lpvData))
         return FALSE;
   }

   pData = (DWORD *) lpvData; // Cast to my data type.
   // In this example, it is only a pointer to a DWORD
   // but it can be a structure pointer to contain more complicated data.

   (*pData) = dw;
   return TRUE;
}

extern "C" __declspec(dllexport) BOOL __stdcall WINAPI GetData(DWORD *pdw)
{
   LPVOID lpvData; 
   DWORD * pData;  // The stored memory pointer 

   lpvData = TlsGetValue(dwTlsIndex); 
   if (lpvData == NULL)
      return FALSE;

   pData = (DWORD *) lpvData;
   (*pdw) = (*pData);
   return TRUE;
}



thanks in advance,
George
AnswerRe: Run-Time Check Failure Pin
Naveen30-Sep-08 23:59
Naveen30-Sep-08 23:59 
GeneralRe: Run-Time Check Failure Pin
George_George1-Oct-08 0:21
George_George1-Oct-08 0:21 
GeneralRe: Run-Time Check Failure Pin
toxcct1-Oct-08 0:32
toxcct1-Oct-08 0:32 
GeneralRe: Run-Time Check Failure Pin
George_George1-Oct-08 0:34
George_George1-Oct-08 0:34 
GeneralRe: Run-Time Check Failure Pin
toxcct1-Oct-08 1:14
toxcct1-Oct-08 1:14 
GeneralRe: Run-Time Check Failure Pin
George_George3-Oct-08 0:14
George_George3-Oct-08 0:14 
GeneralRe: Run-Time Check Failure Pin
toxcct3-Oct-08 0:22
toxcct3-Oct-08 0:22 
GeneralRe: Run-Time Check Failure Pin
George_George3-Oct-08 0:37
George_George3-Oct-08 0:37 
AnswerRe: Run-Time Check Failure Pin
Cedric Moonen1-Oct-08 0:17
Cedric Moonen1-Oct-08 0:17 
GeneralRe: Run-Time Check Failure Pin
George_George1-Oct-08 0:23
George_George1-Oct-08 0:23 
GeneralRe: Run-Time Check Failure Pin
Cedric Moonen1-Oct-08 0:53
Cedric Moonen1-Oct-08 0:53 
GeneralRe: Run-Time Check Failure Pin
George_George1-Oct-08 1:01
George_George1-Oct-08 1:01 
GeneralRe: Run-Time Check Failure Pin
Cedric Moonen1-Oct-08 1:12
Cedric Moonen1-Oct-08 1:12 
GeneralRe: Run-Time Check Failure Pin
George_George3-Oct-08 0:12
George_George3-Oct-08 0:12 
GeneralRe: Run-Time Check Failure Pin
Cedric Moonen3-Oct-08 1:42
Cedric Moonen3-Oct-08 1:42 
GeneralRe: Run-Time Check Failure Pin
George_George3-Oct-08 1:51
George_George3-Oct-08 1:51 
QuestionSystem Volume Information folder Pin
john563230-Sep-08 23:01
john563230-Sep-08 23:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.