-2

The error "linkage specification is incompatible with previous "MonitorFromWindow"" in 32 bit build VS10 MCBS is given for the following decl:

 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
    HMONITOR MonitorFromWindow(_In_ HWND  hwnd, _In_ DWORD dwFlags);

    return DialogBoxW(hInstance, MAKEINTRESOURCEW(lpTemplate), nullptr, DlgProc);
}

Assumed the solution would be similar to the one given here, but here it might be some problem with the SDK although some kind of header hack is preferred.

Community
  • 1
  • 1
Laurie Stearn
  • 959
  • 1
  • 13
  • 34
  • You've missed the introductory text that explains what you are doing, what you are compiling, what your code is, and how to reproduce the error. – David Heffernan Mar 01 '16 at 11:00
  • @David: Is the edit sufficient? The obvious problem is that we don't have a hwnd yet, but I am sure that problem is unrelated to the error. – Laurie Stearn Mar 01 '16 at 11:07
  • 2
    You *declared* MonitorFromWindow, what was the point of that?? Sure, it is not compatible with the real declaration, which is __declspec(dllimport) and uses the __stdcall calling convention. Did you mean to call it? – Hans Passant Mar 01 '16 at 11:15
  • In unchartered territory here,It has been recommended to check for multiple displays [here](http://stackoverflow.com/questions/35669717/dialogex-with-controls-resizing/35677109#35677109) - and which display uses the application. – Laurie Stearn Mar 01 '16 at 11:20

1 Answers1

2

The error is telling you that your declaration of MonitorFromWindow conflicts with the prior declaration. The prior declaration in Winuser.h declared the function with extern "C" linkage, is __declspec(dllimport) and has the __stdcall calling convention.

You should remove your erroneous declaration and use that from the header file.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Interesting: Winuser.h tells me: `WINUSERAPI HMONITOR WINAPI MonitorFromWindow( __in HWND hwnd, __in DWORD dwFlags);` Does that conform to your assertion? – Laurie Stearn Mar 01 '16 at 11:15
  • Ah: the double underscore is stdcall: got it. – Laurie Stearn Mar 01 '16 at 11:16
  • What is hard to understand is you would try to declare this function at all rather than use the header file? – David Heffernan Mar 01 '16 at 11:17
  • We get the "type name is not allowed" for HWND when calling the function. – Laurie Stearn Mar 01 '16 at 11:23
  • Well, I cannot see your code that calls the function, so I cannot comment on that. All I've done is answered the question that you asked. – David Heffernan Mar 01 '16 at 11:25
  • Thanks David, to call the function all that is tried is to remove HMONITOR in the above code, and compiled it with and without the "__In__". is that for another question, because a solution here does not appear obvious. – Laurie Stearn Mar 01 '16 at 11:28
  • Remove that declaration entirely, and call the function like this: `HMONITOR hMon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);`. It looks like you have some basic misunderstandings on how functions are declared, and how they are called. I suggest that you revisit your C++ text book. – David Heffernan Mar 01 '16 at 11:29
  • Thanks for the info. The [MSDN page](https://msdn.microsoft.com/en-au/library/windows/desktop/dd145064(v=vs.85).aspx) wants updating perhaps? – Laurie Stearn Mar 01 '16 at 11:31
  • No, the MSDN page is accurate. What do you feel is wrong with it? – David Heffernan Mar 01 '16 at 11:32
  • Oops sorry to have wasted your time. I guess the MSDN page could have an example or community comment section. Also an included link to a list of parameters like the suggested MONITOR_DEFAULTTONEAREST. – Laurie Stearn Mar 01 '16 at 11:41
  • It does describe the available flags. There are listed right there. There are some basic pre-requisites for reading documentation like this. I don't think it would help if every such documentation topic had to start from the ground floor. Understanding how to call a function, and what a window handle is are pre-requisites here. – David Heffernan Mar 01 '16 at 11:43
  • I understand. I've been calling functions (correctly and incorrectly) for years- probably longer than you? :), but never got that linkage error of which one of the few documented cases are [here](http://stackoverflow.com/questions/3685773/vs2010-compiler-and-cuda-error-linkage-specification-is-incompatible-with-previ). – Laurie Stearn Mar 01 '16 at 11:49
  • 1
    @LaurieStearn: You still don't seem to quite understand. Your code is buggy. You are **not** calling a function. You are **declaring** a function (and doing it wrong, while at it, hence the error message). There is no issue with library files or the MSDN documentation. You simply don't seem to understand **very basic** concepts of C programming, case in point being: How to call a function. Getting this wrong after years of calling functions leaves me somewhat astonished. – IInspectable Mar 01 '16 at 14:54
  • IInspectable: Always pasted func decls in before using them. That's my way. And I know the errors that come up. But as the linkage error had *very* poor documentation, and as I was encouraged by Hans to open a new thread, (see above post) we get shot down in flames. Grrrr. – Laurie Stearn Mar 02 '16 at 05:46
  • You should stop pasting function declarations. Let them be declared in header files which you include. – David Heffernan Mar 02 '16 at 09:47