Answer the question
In order to leave comments, you need to log in
What is the cause of the error when connecting the COM port via dll?
I'm making a dll to connect a device via a COM port. I can't figure out what is causing the error.
There are no problems with calling a function from a dll. When debugging, I can go into it through F11 and watch the progress, but when I try to open a COM port with CreateFile, it drops out
First step of handling exception at address 0x00021478 in dll_check.exe: 0xC0000005: Execution Access Violation at address 0x00021478.
Unhandled exception at 0x00021478 in dll_check.exe: 0xC0000005: Access violation while executing at 0x00021478.
extern "C" _declspec(dllexport) int connectAr(){
HANDLE hSerial;
LPCTSTR sPortName = L"COM3";
hSerial = ::CreateFile(sPortName, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
//ошибка выпадает при попытке вызова CreateFile
HINSTANCE hDllInstance = LoadLibraryEx(L"mydll.ll", 0, DONT_RESOLVE_DLL_REFERENCES);
typedef int(*functionDll)();
functionDll fpFunction = (functionDll)GetProcAddress(hDllInstance, "connectAr");
int n = fpFunction();
Answer the question
In order to leave comments, you need to log in
Problem solved. I connected the library through LoadLibrary, not through LoadLibraryEx.
It was in the DONT_RESOLVE_DLL_REFERENCES parameter. Therefore, if inside the dll you need to open, for example, a COM port, you must use either LoadLibrary, or use LOAD_WITH_ALTERED_SEARCH_PATH in the LoadLibraryEx function.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question