0
votes

Here is my code:

#include "stdafx.h"
#include <Windows.h>

extern "C" int __stdcall myfunction ();

BOOL WINAPI DllMain ( HINSTANCE hin, DWORD reason, LPVOID lpvReserved );

int __stdcall myfunction ()
{
      MessageBoxW(NULL,L"Question",L"Title",MB_OK);
      return 0;
}

BOOL WINAPI DllMain ( HINSTANCE hin, DWORD reason, LPVOID lpvReserved )
{
    return TRUE;
}

When i compile show these errors:

error LNK2028: reference to simbol (token) unresolved (0A000027) "extern "C" int stdcall MessageBoxW(struct HWND *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBoxW@@$$J216YGHPAUHWND__@@PB_W1I@Z) in the function "extern "C" int __stdcall myfunction(void)" (?myfunction@@$$J10YGHXZ)

error LNK2019: External symbol "extern "C" int stdcall MessageBoxW(struct HWND *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBoxW@@$$J216YGHPAUHWND__@@PB_W1I@Z) unresolved used in the function "extern "C" int __stdcall myfunction(void)" (?myfunction@@$$J10YGHXZ)

I dont understand where is the error and their cause. If someone can help me to fix it I will thanks alot :)

2

2 Answers

1
votes

Thanks everyone, but the problem was the user32.lib.

#include "stdafx.h"
#include <Windows.h>

#pragma comment(lib,"user32.lib"); //Missing lib (No compile errors)

BOOL __stdcall DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved) {
    return  TRUE;
}

extern "C" __declspec(dllexport) void __stdcall SomeFunction() {
    MessageBoxA(NULL, "Hello", "HELLO", 0x000000L); //0x000000L = MB_OK
}

I hope this gonna to be helpful for noobs like me.

0
votes

The extern "C" needs to be on the function, not the definition:

int __stdcall myfunction ();

extern "C" int __stdcall myfunction ()
{
      MessageBoxW(NULL, L"Question", L"Title", MB_OK);
      return 0;
}

however, instead of attaching extern "C" to it all, you could wrap it in a pre-parser conditional:

int __stdcall myfunction ();

#ifdef __cplusplus
    extern "C" {
#endif

int __stdcall myfunction ()
{
      MessageBoxW(NULL, L"Question", L"Title", MB_OK);
      return 0;
}

#ifdef __cplusplus
    }
#endif