creating error or loading DLL error?
-
Hello to all, I am using Visual C++ Express 2008, now I am trying to create an DLL and use it next. I think I do everything allright, compile the code. This code creates a DLL file. Next, I created code to use that DLL, after compile there is no errors. During debug, it appears this error: Unhandled exception at 0x00000000 in useDLL1.exe: 0xC0000005: Access violation reading location 0x00000000. In this moment, I don't know if the mistake is from the DLL file or from the following code( it is for loads DLL ). I need, and I appreciate some help to understand where is the problem. ---------------------------------------------------------------- #include #include using namespace std; typedef int (CALLBACK* DLLfunc)(int); #pragma once int main() { int i, result; HINSTANCE loadFcn = LoadLibrary("createDLL1.dll"); // Check to see if the library was loaded successfully if (loadFcn != 0) cout << "\n *LoadMe library loaded!\n"; else cout << "\n *LoadMe library failed to load!\n"; DLLfunc loadedFcn; loadedFcn = (DLLfunc)GetProcAddress(loadFcn,"functeste"); result=loadedFcn(30); //result = 10; cout << "hello DLL" << endl << endl; cout << loadedFcn; cout << endl << result; cin >> i; return 0; } ------------------------------------------------------------------------
-
Hello to all, I am using Visual C++ Express 2008, now I am trying to create an DLL and use it next. I think I do everything allright, compile the code. This code creates a DLL file. Next, I created code to use that DLL, after compile there is no errors. During debug, it appears this error: Unhandled exception at 0x00000000 in useDLL1.exe: 0xC0000005: Access violation reading location 0x00000000. In this moment, I don't know if the mistake is from the DLL file or from the following code( it is for loads DLL ). I need, and I appreciate some help to understand where is the problem. ---------------------------------------------------------------- #include #include using namespace std; typedef int (CALLBACK* DLLfunc)(int); #pragma once int main() { int i, result; HINSTANCE loadFcn = LoadLibrary("createDLL1.dll"); // Check to see if the library was loaded successfully if (loadFcn != 0) cout << "\n *LoadMe library loaded!\n"; else cout << "\n *LoadMe library failed to load!\n"; DLLfunc loadedFcn; loadedFcn = (DLLfunc)GetProcAddress(loadFcn,"functeste"); result=loadedFcn(30); //result = 10; cout << "hello DLL" << endl << endl; cout << loadedFcn; cout << endl << result; cin >> i; return 0; } ------------------------------------------------------------------------
-
Hello to all, I am using Visual C++ Express 2008, now I am trying to create an DLL and use it next. I think I do everything allright, compile the code. This code creates a DLL file. Next, I created code to use that DLL, after compile there is no errors. During debug, it appears this error: Unhandled exception at 0x00000000 in useDLL1.exe: 0xC0000005: Access violation reading location 0x00000000. In this moment, I don't know if the mistake is from the DLL file or from the following code( it is for loads DLL ). I need, and I appreciate some help to understand where is the problem. ---------------------------------------------------------------- #include #include using namespace std; typedef int (CALLBACK* DLLfunc)(int); #pragma once int main() { int i, result; HINSTANCE loadFcn = LoadLibrary("createDLL1.dll"); // Check to see if the library was loaded successfully if (loadFcn != 0) cout << "\n *LoadMe library loaded!\n"; else cout << "\n *LoadMe library failed to load!\n"; DLLfunc loadedFcn; loadedFcn = (DLLfunc)GetProcAddress(loadFcn,"functeste"); result=loadedFcn(30); //result = 10; cout << "hello DLL" << endl << endl; cout << loadedFcn; cout << endl << result; cin >> i; return 0; } ------------------------------------------------------------------------
Hi, I think GetProcAddress can't resolve the function address at your DLL. Can you check your DLL if the function is exported from your DLL (e.g. with Depency Walker from VC, I'm not sure that it is included at the express version). Is your DLL-func exported like
extern "C" __declspec(dllexport) int functeste(int i){....}
Take also a look to this article and my comment. http://www.codeproject.com/KB/DLL/rsLoadtimeDLL.aspx?fid=29819&select=1269812&tid=1074998[^] HTH Frankmodified on Friday, February 20, 2009 2:06 AM
-
Hello to all, I am using Visual C++ Express 2008, now I am trying to create an DLL and use it next. I think I do everything allright, compile the code. This code creates a DLL file. Next, I created code to use that DLL, after compile there is no errors. During debug, it appears this error: Unhandled exception at 0x00000000 in useDLL1.exe: 0xC0000005: Access violation reading location 0x00000000. In this moment, I don't know if the mistake is from the DLL file or from the following code( it is for loads DLL ). I need, and I appreciate some help to understand where is the problem. ---------------------------------------------------------------- #include #include using namespace std; typedef int (CALLBACK* DLLfunc)(int); #pragma once int main() { int i, result; HINSTANCE loadFcn = LoadLibrary("createDLL1.dll"); // Check to see if the library was loaded successfully if (loadFcn != 0) cout << "\n *LoadMe library loaded!\n"; else cout << "\n *LoadMe library failed to load!\n"; DLLfunc loadedFcn; loadedFcn = (DLLfunc)GetProcAddress(loadFcn,"functeste"); result=loadedFcn(30); //result = 10; cout << "hello DLL" << endl << endl; cout << loadedFcn; cout << endl << result; cin >> i; return 0; } ------------------------------------------------------------------------
From
GetProcAddress
documentation [^]:Return Value If the function succeeds, the return value is the address of the exported function or variable. If the function fails, the return value is NULL. To get extended error information, call GetLastError.
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hello to all, I am using Visual C++ Express 2008, now I am trying to create an DLL and use it next. I think I do everything allright, compile the code. This code creates a DLL file. Next, I created code to use that DLL, after compile there is no errors. During debug, it appears this error: Unhandled exception at 0x00000000 in useDLL1.exe: 0xC0000005: Access violation reading location 0x00000000. In this moment, I don't know if the mistake is from the DLL file or from the following code( it is for loads DLL ). I need, and I appreciate some help to understand where is the problem. ---------------------------------------------------------------- #include #include using namespace std; typedef int (CALLBACK* DLLfunc)(int); #pragma once int main() { int i, result; HINSTANCE loadFcn = LoadLibrary("createDLL1.dll"); // Check to see if the library was loaded successfully if (loadFcn != 0) cout << "\n *LoadMe library loaded!\n"; else cout << "\n *LoadMe library failed to load!\n"; DLLfunc loadedFcn; loadedFcn = (DLLfunc)GetProcAddress(loadFcn,"functeste"); result=loadedFcn(30); //result = 10; cout << "hello DLL" << endl << endl; cout << loadedFcn; cout << endl << result; cin >> i; return 0; } ------------------------------------------------------------------------
In first, I want to thank all help. - ns ami - : I already tryed that. Debug stops on line: "result=loadedFcn(30);" I think that is when this command tries to access to function into DLL. Frank: Your tip was very fine. I did that. "Depency Walker" didn´t get to read the DLL. The problem seems to me, that is in DLL. Now i'm trying to fix it. ...Another problem :). I made some alterations, and next I run "Depency Walker". This time get to read the DLL. But when I run debug, that code above,it occur the same error: First-chance exception at 0x00000000 in useDLL1.exe: 0xC0000005: Access violation reading location 0x00000000. Unhandled exception at 0x00000000 in useDLL1.exe: 0xC0000005: Access violation reading location 0x00000000. CPallini: I have tried your tip, and I added this line of code below DWORD err = GetLastError(); it appears err = 127 ?? ( this meants it can't find the file ?? why? ) Could you help me? Anyway, thanks for your concern.
-
In first, I want to thank all help. - ns ami - : I already tryed that. Debug stops on line: "result=loadedFcn(30);" I think that is when this command tries to access to function into DLL. Frank: Your tip was very fine. I did that. "Depency Walker" didn´t get to read the DLL. The problem seems to me, that is in DLL. Now i'm trying to fix it. ...Another problem :). I made some alterations, and next I run "Depency Walker". This time get to read the DLL. But when I run debug, that code above,it occur the same error: First-chance exception at 0x00000000 in useDLL1.exe: 0xC0000005: Access violation reading location 0x00000000. Unhandled exception at 0x00000000 in useDLL1.exe: 0xC0000005: Access violation reading location 0x00000000. CPallini: I have tried your tip, and I added this line of code below DWORD err = GetLastError(); it appears err = 127 ?? ( this meants it can't find the file ?? why? ) Could you help me? Anyway, thanks for your concern.
Thanks to all, again. I have reviewed all code and responses about this. And I added some lines of code. In the creation of DLL: ( extern "C" __declspec(dllexport) int functest(int i){....} ) And, using DLL: typedef int (*MYFUNC)(int); MYFUNC pnMyfunc = (MYFUNC)loadedFcn; cout << " value of dll func = " << pnMyfunc(36) << endl; FreeLibrary(loadFcn); Next all DLL code: #include #include using namespace std; typedef int (CALLBACK* DLLfunc)(int); typedef int (*MYFUNC)(int); // --------------------------------- new line #pragma once int main() { int i; DWORD err; HINSTANCE loadFcn = LoadLibrary("createDLL1.dll"); err = GetLastError(); // Check to see if the library was loaded successfully if (loadFcn != 0){ cout << "\n *LoadMe library loaded!\n"; DLLfunc loadedFcn; loadedFcn = (DLLfunc)GetProcAddress(loadFcn,"functest"); MYFUNC pnMyfunc = (MYFUNC)loadedFcn; // -------------- new line cout << endl << "hello DLL" << endl; cout << " value of dll func = " << pnMyfunc(36) << endl; cout << endl << " DLL func address " << *loadedFcn;} else if (err){ cout << " erro - " << err; cout << "\n *LoadMe library failed to load!\n";} FreeLibrary(loadFcn); cout << endl; cout << endl << " **Free memory** " << endl; cin >> i; return 0; }