Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ LoadLibrary throws First-Chance Exception, yet works?
    primarykey
    data
    text
    <p>I finished my small application and I am trying to make sure I have no memory leaks and no bugs. After looking at my Output I noticed that one of my functions is throwing a First-Chance exception, yet the function works great and does not crash.</p> <p>The function calls another function in a CLR C++ DLL. I removed almost all code in the DLL function just for a test and the exception is still thrown so I know it is my EXE function that is the issue. </p> <p>This is the code for the EXE function to call the DLL function.</p> <pre><code>LPCTSTR CHAXC0RDlg::Encrypt(LPCTSTR strValue) { const char* Return; HINSTANCE hDLL = LoadLibrary(L"Library.dll"); if(hDLL) { FARPROC hMethod = GetProcAddress(HMODULE (hDLL), "Encrypt"); if(hMethod) { typedef const char* (*FunctionSig)(LPCTSTR); FunctionSig MethodCall = FunctionSig(hMethod); Return = MethodCall(strValue); FreeLibrary(hDLL); } } return _tcsdup(CString(Return)); } </code></pre> <p>This is the DLL function (as you can see I removed all code except for the code that generates a return value just as a test):</p> <pre><code>const char* Encrypt(LPCTSTR strPValue) { String^ strValue = gcnew String(strPValue); string strReturn = (const char*)(Marshal::StringToHGlobalAnsi(strValue)).ToPointer(); char* csValue = new char[strReturn.size()]; strcpy(csValue, strReturn.c_str()); return const_cast&lt;const char*&gt; (csValue); } </code></pre> <p>The EXE function throws the exception on "<code>const char* Return = MethodCall(strValue);</code>" (I enabled breaking on this exception that is how I know).</p> <p>Why exactly is this function throwing this exception?</p> <p>Thank You!</p> <p><strong>EDIT</strong></p> <p>Update: My Character Set is UNICODE.</p> <p>Update #2: From what I read in the suggestions and answers you are assuming this code does not work but it does. I enabled break on first-chance exceptions (and yes I do know what a first chance exception is) because I wanted this program to be good quality with all bugs gone. The code runs fine, I just wanted to figure out why the first-chance exception is being thrown because I like to be a better programmer. So I would like to fix this.</p> <p>Update #3: I now have my code checking the values of hDLL and hMethod and both are not null when running this function. The problem seems to be in the call it self to the DLL. I am assuming the function signature is 100% correct for this code does work, it just throws a first-chance exception.</p> <p>Update #4: I added the new changes to my function above as well added the DLL function code. The DLL function is a CLR C++ DLL. I removed all code in the DLL function as I stated already to make sure it was not my DLL.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload