When you try to register the library gives an error

Exeption_Search.cpp:

#include "stdafx.h" #include <iostream> #include <Windows.h> extern "C" __declspec(dllexport) void OnConnect() { // {5994FCF1-EC8C-48CE-9078-3C4DE11FFD4E} static const GUID GUID_handler_no_null = { 0x5994fcf1, 0xec8c, 0x48ce,{ 0x90, 0x78, 0x3c, 0x4d, 0xe1, 0x1f, 0xfd, 0x4e } }; MessageBox( NULL, L"Пустое значение", 0, MB_OK ); std::cout << "handler_no_null"; } 

dllmain.cpp:

 #include "stdafx.h" HINSTANCE g_hInst = NULL; BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } STDAPI DllRegisterServer(void) { return 0; } 

On the Internet, I did not find a detailed and understandable solution to the problem.

  • And what does the import table look like? DllRegisterServer exported or not? What is the library bit and regsrv32? - VTT
  • While suffering with this, I read somewhere that def is not needed if you use extern "C" __ declspec (dllexport). This is not true? - medyaniklis
  • I meant the table * export in the library itself. And so, yes, you can do without the .def file. - VTT
  • I did not know about her until you said. I'll try to figure it out. I think more questions will arise. At once a question in dogonku - the program for which I need a library, so far that it "does not see". Did I enter the wrong GUID or is it a result of a failed registration? - medyaniklis
  • Rather, the consequences of unsuccessful registration. - VTT

1 answer 1

The STDAPI macro unfolds in extern "C" HRESULT __stdcall , i.e. it does not in itself specify the export of a function from a DLL. To export a function, it is enough to add __declspec(dllexport) , but for regsvr32 this will not help, since with __stdcall the function is exported by default with the decorated name, and regsvr32 expects undecorated. For export by undecorated name, you can use either the def file or the export linker parameter (which can be set in the code with the pragma directive).

The decorated name for a function with no arguments has the form _DllRegisterServer@0 — it is easy to get it with the dumpbin /exports command. Then the export code for DllRegisterServer will look like this:

 #pragma comment(linker, "/export:DllRegisterServer=_DllRegisterServer@0") STDAPI DllRegisterServer(void) { return 0; }