GCC Win32 API Linking Issue with ComCtl32 - c

I can't get Comctl32.lib to link with my program using GCC (MinGW).
GCC Input:
gcc -o program.exe main.c images.o -lgdi32 -lcomctl32 -mwindows
GCC Output
main.c: In function 'WinMain':
main.c:120:2: error: unknown type name 'INITCOMMONCONTROLSEX'
main.c:124:9: error: request for member 'dwICC' in something not a structure or union
Related Code in main.c
#define _WIN32_WINNT _WIN32_WINNT_WIN7
#include <windows.h>
#include <commctrl.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmd)
{
Line 120: INITCOMMONCONTROLSEX icex;
Line 124: icex.dwICC = ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&icex);
}
Thank you for any help or information you can provide. I've been at this too long and just can't come up with an answer.

I have resolved the issue thanks to David H. I had to define
#define _WIN32_IE 0x0900
I was under the impression that commctrl.h was defining 0x0500 by default (0x0300 is required for my functions), but it appears it wasn't.
If you do not define the _WIN32_IE macro in your project, it is automatically defined as 0x0500. - MSDN Source

Related

mingw g++ Windows Subsystem WinMain not getting hInstance value

I'm converting a VS2015 C++ directx/winforms app to VS code using mingw G++ (both on windows 10).
I have it compiling and linking after adding the -mwindows option (and a whole bunch of libraries) but upon stepping into the WinMain there is no value in hInstance.
#include <objbase.h>
#include <windows.h>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
LoadString(hInstance, IDS_APP_TITLE,g_szAppName, MAX_LOADSTRING);
...
Which results in anything depending on it like LoadString or RegisterClassEx not working.
What should I be looking for?

Creating an IWebBrowser2 control

I just want to successfully add it to my window, and it has been surprisingly difficult.
I've tried
#include "windef.h"
#include "winbase.h"
#include "initguid.h"
#include "ole2.h"
#include "olectl.h"
#include "shobjidl.h"
#include "shlguid.h"
#include "exdispid.h"
#include <objidl.h>
#include "OleIdl.h"
#include "Objbase.h"
#include <exdisp.h>
#include <exdispid.h>
...
IWebBrowser2* pBrowser2;
HRESULT hr = CoCreateInstance(CLSID_InternetExplorer, NULL,
CLSCTX_ALL, IID_IWebBrowser2, (void**)&pBrowser2);
Getting
error: 'CLSID_InternetExplorer' undeclared (first use in this function)
HRESULT hr = CoCreateInstance(CLSID_InternetExplorer,
I've also tried
CoCreateInstance(CLSID_WebBrowser, NULL, CLSCTX_LOCAL_SERVER,
IID_IWebBrowser2, (void**)&pBrowser2);
This one at least compiles, but nothing is added to the window:
hr = OleCreate(&CLSID_WebBrowser, &IID_IOleObject, 1/*OLERENDER_DRAW*/, 0,
&ClientSite, &Storage, (void**)&mpWebObject);
I tried all headers and libraries I could find on the net (as you can see).
Here are the libraries I link:
gcc -lmingw32 -mwindows -luser32 -lgdiplus -lole32 -luuid -loleaut32 -lcomctl32 -lcomdlg32 -ladvapi32 -loleaut32 -lshdocvw -lmf -lmfuuid
Thanks!
You could try MinGW-w64.
This is a fork of MinGW which , in addition to supporting both 32-bit and 64-bit builds, is under much more active development. In particular, having improved Windows API headers.
Apparently MinGW doesn't support IWebBrowser2. The code worked fine in Visual Studio.
Start by including these header files first:
#include <windows.h>
#include <objbase.h>
#include <ExDisp.h>
#include <ExDispid.h>
Then this:
IWebBrowser2* pBrowser2 = nullptr;
HRESULT hr;
hr = CoCreateInstance(__uuidof(WebBrowser), NULL, CLSCTX_INPROC, __uuidof(IWebBrowser2), (void**)pBrowser2);
The use of __uuidof macro takes resolves the linkage issues to externally defined guids.

argument of type const char* is incompatible with parameter of type "LPCWSTR"

I am trying to make a simple Message Box in C in Visual Studio 2012, but I am getting
the following error messages
argument of type const char* is incompatible with parameter of type "LPCWSTR"
err LNK2019:unresolved external symbol_main referenced in function_tmainCRTStartup
Here is the source code
#include<Windows.h>
int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow)
{
MessageBox(0,"Hello","Title",0);
return(0);
}
Please Help
Thanks and Regards
To compile your code in Visual C++ you need to use Multi-Byte char WinAPI functions instead of Wide char ones.
Set Project -> Properties -> General -> Character Set option to Use Multi-Byte Character Set
I found it here https://stackoverflow.com/a/33001454/5646315
To make your code compile in both modes, enclose the strings in _T() and use the TCHAR equivalents
#include <tchar.h>
#include <windows.h>
int WINAPI _tWinMain(HINSTANCE hinstance, HINSTANCE hPrevinstance, LPTSTR lpszCmdLine, int nCmdShow)
{
MessageBox(0,_T("Hello"),_T("Title"),0);
return 0;
}
I recently ran in to this issue and did some research and thought I would document some of what I found here.
To start, when calling MessageBox(...), you are really just calling a macro (for backwards compatibility reasons) that is calling either MessageBoxA(...) for ANSI encoding or MessageBoxW(...) for Unicode encoding.
So if you are going to pass in an ANSI string with the default compiler setup in Visual Studio, you can call MessageBoxA(...) instead:
#include<Windows.h>
int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow)
{
MessageBoxA(0,"Hello","Title",0);
return(0);
}
Full documentation for MessageBox(...) is located here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx
And to expand on what #cup said in their answer, you could use the _T() macro and continue to use MessageBox():
#include<tchar.h>
#include<Windows.h>
int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow)
{
MessageBox(0,_T("Hello"),_T("Title"),0);
return(0);
}
The _T() macro is making the string "character set neutral". You could use this to setup all strings as Unicode by defining the symbol _UNICODE before you build (documentation).
Hope this information will help you and anyone else encountering this issue.
Yes whatever it was it was a wrong tutorial, you need to make it a long byte integer.
Try this:
#include<Windows.h>
int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow)
{
MessageBox(0,L"Hello",L"Title",0);
return(0);
}

Undefined reference adding to existing file and header

first of all I know this is asked a thousand times. But I opened a few and everyone forgot to compile/link it with it.
Anyway I created a linked list in a separate file with header and file, which works fine, however I tried to add a new function to it but then I have undefined reference to 'function.' Here is the source:
list.c
#include "list.h"
struct node
{
item_t x;
struct node *next;
};
struct node* root;
//Window hider extension
void ToggleVisibleList(HWND currentHwnd)
{
if (root == 0)
return;
struct node *conductor = root;
while (conductor != 0)
{
HWND hwnd = (HWND)conductor->x;
ShowWindow(hwnd, IsWindowVisible(hwnd) ? SW_HIDE : SW_SHOW);
conductor = conductor->next;
}
ShowWindow(currentHwnd, IsWindowVisible(currentHwnd) ? SW_HIDE : SW_SHOW);
}
//...Rest of the file
list.h
#ifndef LIST_H
#define LIST_H
#include <stdlib.h>
#include <windows.h>
//Window hider extension
void ToggleVisibleList(HWND currentHwnd);
//.. rest of the header
main.c
#include <windows.h>
#include <stdio.h>
#include <commctrl.h>
#include "list.h"
HWND currentHwnd;
//..
HHOOK hookKeyboard;
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode >= 0)
{
if (wParam == WM_KEYDOWN)
{
KBDLLHOOKSTRUCT* hookStruct = (KBDLLHOOKSTRUCT*)lParam;
if (hookStruct->vkCode == 'Z' && GetAsyncKeyState(VK_LCONTROL))
{
ToggleVisibleList(currentHwnd);
}
}
}
CallNextHookEx(hookKeyboard, nCode, wParam, lParam);
}
//..Rest of file
I compile using Mingw (OS: Windows 8 64-bit):
gcc -o hider.exe main.c list.c -mwindows
C:\Users\...\AppData\Local\Temp\cc6sCa17.o:main.c:(.text+0x4bc): undefined reference to `ToggleVisibleList'
collect2: ld gaf exit-status 1 terug
//Translation: ld return exit-status 1
EDIT: Tried swapping the file order.
I hope I didn't duplicate a question, I don't think so because I've tried 20 questions first. (And google.)
Regards
Answer: rebooted my computer and it compiled.
Total shot in the dark answer:
I suspect this might work (swap list.c and main.c in the compile order)
gcc -o hider.exe list.c main.c -mwindows
I suggest this because linking with libraries has similar behavior with gcc. But I've never observed this problem with file order.
Fromt the gcc man page (in regards to -l library)
It makes a difference where in the command you write this option; the
linker searches and processes
libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library
z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be
loaded.

Can't link against WICConvertBitmapSource

I'd like to use a function from WindowsCodecs.dll, but MinGW has incomplete and missing WinAPI headers, as well as import libraries. Consider the following demo:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
// ---------- dummy declarations, because MinGW got no wincodec.h ----------
typedef REFGUID REFWICPixelFormatGUID;
typedef VOID IWICBitmapSource;
HRESULT WINAPI WICConvertBitmapSource(
REFWICPixelFormatGUID dstFormat,
IWICBitmapSource *pISrc,
IWICBitmapSource **ppIDst);
// -------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpLine, int nShow)
{
#ifdef LOAD_FROM_DLL
typedef HRESULT (WINAPI *PWICConvertBitmapSource)(
REFWICPixelFormatGUID, IWICBitmapSource *, IWICBitmapSource **);
HMODULE hDll = LoadLibrary("WindowsCodecs.dll");
PWICConvertBitmapSource pFunc =
(PWICConvertBitmapSource)GetProcAddress(hDll, "WICConvertBitmapSource");
printf("WICConvertBitmapSource: 0x%p.\n", pFunc);
pFunc(NULL, NULL, NULL);
FreeLibrary(hDll);
#else
WICConvertBitmapSource(NULL, NULL, NULL);
#endif
return 0;
}
When built by gcc test.c -DLOAD_FROM_DEF, the program prints address of the function and terminates correctly. Although, when linked against the import library from the following def:
LIBRARY WindowsCodecs.dll
EXPORTS
WICConvertBitmapSource#12
, this error pops out:
The procedure entry point WICConvertBitmapSource#12 could
not be located in the dynamic link library WindowsCodecs.dll.
Surprisingly enough, if I remove the declaration of WICConvertBitmapSource from the source and #12 from def file, the program links and runs fine.
How can I create a correct import library?
Notes: I'm running MinGW on Windows 7 SP1. My gcc version is 4.7.0 with w32api 3.17 installed. The problem appears with many functions, like GdiAlphaBlend, or SHCreateStreamOnFileEx.
The import library should've been created with --kill-at flag, like this:
dlltool --kill-at -D WindowsCodecs.dll -d WindowsCodecs.def -l libwindowscodecs.a
This article clarified everything for me: http://wyw.dcweb.cn/stdcall.htm

Resources