A
A
amberav2014-08-12 16:41:18
C++ / C#
amberav, 2014-08-12 16:41:18

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.

A piece of function code from a dll.
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

It seems that I looked through the books on winAPI, but nothing similar is described there. I have 2 options. Either the error is related to pointers, but I don't know what can be fixed here, or something with SECURITY_ATTRIBUTES.
If I do not make a dll, but an executable, then there are no connection problems. COM port is working. That is, the problem may still be related to calling the winapi function not directly, but from a dll.
Calling a dll function
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

1 answer(s)
A
amberav, 2014-08-12
@amberav

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 question

Ask a Question

731 491 924 answers to any question