Problem with the WINAPI - Violation


today i started programming a little for Windows.
So my first application should be a tool for listing all windows and getting the name of the executables.

I read a lot about it. And it seems that there are some problems with Windows 7. But i dont know how i can solve this.

I get the following error message:
unhandled exeption at 0x77B6016E (ntdll.dll) in ConsoleApplication2.exe: 0x00000000

<EDIT> Forgot to tell the line of the occurance: Its GetWindowModuleFileName </EDIT>

Here is my Code:

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>

BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam);

int _tmain(int argc, _TCHAR* argv[])
		EnumWindows(EnumChildProc, 0); 

	return 0;

BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam){
	 printf("Window Handle: ");
	 printf("%d", hwnd );

	 LPWSTR name = L"";
	 GetWindowModuleFileName(hwnd, name, 100);
	 printf("Window Title: ");

     return TRUE; 

Do you have any Idea?

best regards
Last edited on
LPWSTR is a pointer, not a string. It's basically a typedef for wchar_t*.

GetWindowModuleFileNameW will write the string data to whatever buffer you provide to it. But your 'name' buffer is not a buffer, but is just a pointer to some random area in memory (and likely read-only memory at that -- so when the function tries to write to it, it could explode).

You need to give the function an actual buffer:

wchar_t name[100];  // if you want to be able to have 100 characters, you need a buffer of at least 100

GetWindowModeulFileNameW( hwnd, name, 100 );  // if using wide characters, use the wide
  // version of the function (note the W at the end) 
Thank you. I think there there is a bug in my visual studio. Because i tried this before, but visual Studio always say "incompatible type".

Now i compiled it whith this "error" and it is working...

But thank you. without you i wouldnt have tried it again ;-)
Topic archived. No new replies allowed.