Error:
Severity Code Description Project File Line Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
Severity Code Description Project File Line
Error LNK1120 1 unresolved externals
Code:
#include "windows.h"
#include "tchar.h"
#include "d3d9.h"
#pragma comment(lib, "d3d9.lib")
LPDIRECT3D9 pDirect3D=NULL;
LPDIRECT3DDEVICE9 pDirect3DDevice=NULL;
const int segment = 50;
const int NV = segment*13;
struct CUSTOMVERTEX
{
float x, y, z, rhv;
DWORD color;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
LPDIRECT3DVERTEXBUFFER9 pVertexBuffer=NULL;
HRESULT InitialDirect3D(HWND hvnd)
{
if((pDirect3D=Direct3DCreate9(D3D_SDK_VERSION))==NULL)
return E_FAIL;
D3DDISPLAYMODE Display;
if(FAILED(pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display)))
return E_FAIL;
D3DPRESENT_PARAMETERS Direct3DParameter;
ZeroMemory(&Direct3DParameter, sizeof Direct3DParameter);
Direct3DParameter.Windowed=TRUE;
Direct3DParameter.SwapEffect=D3DSWAPEFFECT_DISCARD;
Direct3DParameter.BackBufferFormat=Display.Format;
if(FAILED(pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hvnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&Direct3DParameter, &pDirect3DDevice)))
return E_FAIL;
return S_OK;
}
HRESULT RenderingDirect3D()
{
if(pDirect3DDevice==NULL)
return E_FAIL;
pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(90, 150, 100), 1.f, 0);
pDirect3DDevice->BeginScene();
pDirect3DDevice->SetStreamSource(0, pVertexBuffer, 0, sizeof(CUSTOMVERTEX));
pDirect3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
pDirect3DDevice->DrawPrimitive(D3DPT_LINELIST, 0, NV);
pDirect3DDevice->EndScene();
pDirect3DDevice->Present(NULL, NULL, NULL, NULL);
return S_OK;
}
void DeleteDirect3D()
{
if(pVertexBuffer)
pVertexBuffer->Release();
if(pDirect3DDevice)
pDirect3DDevice->Release();
if(pDirect3D)
pDirect3D->Release();
}
HRESULT InitialVertexBuffer()
{
float u = 0.0;
int z = 0;
float points[][2] = {
{150,150},
{125,100},
{150,50},
{200,50},
{225,55},
{285,60},
{325,25},
{342,30},
{340,35},
{340,40},
{325,75},
{300,125},
{300,175},
{305,250},
{295,287},
{257,347},
{200,350},
{150,325},
{140,290},
{142,282},
{160,280},
{165,286},
{175,325},
{215,340},
{250,335},
{275,300},
{275,250},
{255,200},
{250,150},
{275,100},
{305,65},
{275,85},
{240,95},
{215,85},
{170,65},
{140,90},
{160,135},
{160,150},
{152.5,150},
{150,150}
};
CUSTOMVERTEX Vertexes[NV*2];
int i = 0;
while( i < NV*2 )
{
u = 0.f;
for(int j = 0; j < segment; j++)
{
Vertexes[i].x = (1-3*u+3*u*u-u*u*u)*points[z][0]+(3*u-6*u*u+3*u*u*u)*points[z+1][0]+(3*u*u-3*u*u*u)*points[z+2][0]+(u*u*u)*points[z+3][0];
Vertexes[i].y = (1-3*u+3*u*u-u*u*u)*points[z][1]+(3*u-6*u*u+3*u*u*u)*points[z+1][1]+(3*u*u-3*u*u*u)*points[z+2][1]+(u*u*u)*points[z+3][1];
Vertexes[i].z = 0.5f;
Vertexes[i].color = 0x00ffffff;
Vertexes[i].rhv = 1.f;
u += (float)1/segment;
i++;
Vertexes[i].x = (1-3*u+3*u*u-u*u*u)*points[z][0]+(3*u-6*u*u+3*u*u*u)*points[z+1][0]+(3*u*u-3*u*u*u)*points[z+2][0]+(u*u*u)*points[z+3][0];
Vertexes[i].y = (1-3*u+3*u*u-u*u*u)*points[z][1]+(3*u-6*u*u+3*u*u*u)*points[z+1][1]+(3*u*u-3*u*u*u)*points[z+2][1]+(u*u*u)*points[z+3][1];
Vertexes[i].z = 0.5f;
Vertexes[i].color = 0x00ffffff;
Vertexes[i].rhv = 1.f;
i++;
}
z += 3;
}
if(FAILED(pDirect3DDevice->CreateVertexBuffer(NV*2*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &pVertexBuffer, NULL)))
return E_FAIL;
void *pVB=NULL;
if(FAILED(pVertexBuffer->Lock(0, sizeof Vertexes, (void**)&pVB, 0)))
return E_FAIL;
memcpy(pVB, Vertexes, sizeof Vertexes);
pVertexBuffer->Unlock();
return S_OK;
}
LRESULT CALLBACK MainWinProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
switch(msg)
{
case WM_PAINT:
RenderingDirect3D();
ValidateRect(hwnd, NULL);
break;
case WM_DESTROY:
DeleteDirect3D();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX windowsclass;
HWND hwnd;
MSG msg;
//
windowsclass.cbSize=sizeof(WNDCLASSEX);
windowsclass.style=CS_DBLCLKS|CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
windowsclass.lpfnWndProc=MainWinProc;
windowsclass.cbClsExtra=0;
windowsclass.cbWndExtra=0;
windowsclass.hInstance=hinstance;
windowsclass.hIcon=LoadIcon(NULL, IDI_APPLICATION);
windowsclass.hCursor=LoadCursor(NULL, IDC_ARROW);
windowsclass.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);
windowsclass.lpszMenuName=NULL;
windowsclass.lpszClassName=_T("WINDOWSCLASS");
windowsclass.hIconSm=LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&windowsclass))
return 0;
if(!(hwnd=CreateWindowEx(
NULL,
_T("WINDOWSCLASS"),
_T("Desenam litera G"),
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0,
NULL,
NULL,
hinstance,
NULL)))
return 0;
if(SUCCEEDED(InitialDirect3D(hwnd)))
{
if(SUCCEEDED(InitialVertexBuffer()))
{
ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);
ZeroMemory(&msg, sizeof msg);
while(msg.message!=WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
RenderingDirect3D();
}
}
}
return msg.wParam;
}
Check project configuration. Linker->System->SubSystem should be Windows.
If you use CMake you have to set WIN32 flag in add_executable
add_executable(${name} WIN32 ${source_files})
See CMake Doc for more information.
I had same Problem when i was trying to create executable from program that having no main() method. When i included sample main() method like this
int main(){
return 0;
}
It solved
Right click on project. Properties->Configuration Properties->General->Linker.
I found two options needed to be set.
Under System: SubSystem = Windows (/SUBSYSTEM:WINDOWS)
Under Advanced: EntryPoint = main
I faced the same problem too and I found out that I selected "new Win32 application" instead of "new Win32 console application". Problem solved when I switched. Hope this can help you.
just add:
int main()
{
//you can leave it empty
return 0;
}
to your project and everything will work
its causes because of visual studio try to find the main function to start the project and didn't found it
Similar to #仲耀晖 I had the wrong application type configured for a dll. I guess that the project type changed due to some bad copy pasting, as #Daniel Struhl suggested.
How to verify:
Right click on the project -> properties -> Configuration Properties -> General -> Project Defaults -> Configuration Type.
Check if this field contains the correct type, e.g. "Dynamic Library (.dll)" in case the project is a dll.
This is an edge case, but you can also get this error if you are building an MFC application with CMake.
In that case, you need to add the following definitions:
ADD_DEFINITIONS(-D_AFXDLL)
SET(CMAKE_MFC_FLAG 2) # or 1 if you are looking for the static library
If you are compiling with unicode, the following properties also need to be added:
set_target_properties(MyApp PROPERTIES
COMPILE_DEFINITIONS
_AFXDLL,_UNICODE,UNICODE,_BIND_TO_CURRENT_CRT_VERSION,_BIND_TO_CURRENT_MFC_VERSION
LINK_FLAGS "/ENTRY:\"wWinMainCRTStartup\""
)
Soure: FAQ: How to use MFC with CMake
Select the project. Properties->Configuration Properties->Linker->System.
My problem solved by setting below option.
Under System: SubSystem = Console(/SUBSYSTEM:CONSOLE)
Or you can choose the last option as "inherite from the parent".
This worked for me:
(I don't have enough rep to embed pictures yet -- sorry about this.)
I went into Project --> Properties --> Linker --> System.
IMG: Located here, as of Dec 2019 Visual Studio for Windows
My platform was set to Active(Win32) with the Subsystem as "Windows".
I was making a console app, so I set it to "Console".
IMG: Changing "Windows" --> "Console"
Then, I switched my platform to "x64".
IMG: Switched my platform from Active(32) to x64
Solution for me is: I clean both the solution and the project. And just rebuild the project.
This error happens because I tried to delete the main file (only keep library files) in the previous build so at the current build the old stuff is still kept in the built directory. That's why unresolved things happened.
"unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ) "
I had to #include <tchar.h> in my windows service application.
I left it as a windows console type subsystem.
The "Character Set" was set to UNICODE.
I deleted the file with main() function in my project when I wanted to change project type to static library.
I Forgot to change Properties -> General -> Configuration Type
from
Application(.exe)
to
Static Library (.lib)
This too gave me the same error.
Old thread but for me it started working (after following all advise above) when i renamed int main(void) to int wmain(void) and removed WIN23 from cmake's add_executable().
If it is a windows system, then it may be because you are using 32 bit winpcap library in a 64 bit pc or vie versa. If it is a 64 bit pc then copy the winpcap library and header packet.lib and wpcap.lib from winpcap/lib/x64 to the winpcap/lib directory and overwrite the existing
The problem might originate from a macro instruction in SDL_main.h
In that macro your main(){} is renamed to SDL_main(){} because SDL needs its own main(){} on some of the many platforms they support, so they change yours. Mostly it achieves their goal, but on my platform it created problems, rather than solved them. I added a 2nd line in SDL_main.h, and for me all problems were gone.
#define main SDL_main //Original line. Renames main(){} to SDL_main(){}.
#define main main //Added line. Undo the renaming.
If you don't like the compiler warning caused by this pair of lines, comment both lines out.
If your code is in WinApp(){} you don't have this problem at all. This answer only might help if your main code is in main(){} and your platform is similar to mine.
I have: Visual Studio 2019, Windows 10, x64, writing a 32 bit console app that opens windows using SDL2.0 as part of a tutorial.
I got the same error because I created a new object from a templated class using the template name without specifying the type explicitly like this:
int main()
{
MyClass<T> Test2(5.60, 6.6); <- This is wrong
^^^
return 0;
}
The correct way to do it was to specify exactly what T was like this:
int main()
{
MyClass<double> Test2(5.60, 6.6); <- This is right
^^^^^^
return 0;
}
for me adding .dll file to my project folder solved this error.
but game me another error: "unresolved external symbol main referenced in function ..." and I solved this by following this answer. https://stackoverflow.com/a/50087608/10903596 or in short adding #define SDL_MAIN_HANDLED in my main file or file containing main function.
You can get this error, if you have your main entry point enclosed in a namespace.
So, don't put your main entry point in a namespace, leave it at the global level.
I wanted to start working with SDL library. After setting and checking the include and library paths for numerous times, I came across the answer by Rinus Verdult here. I added this line after the #include lines and it worked.
#define main main
I have visual studio 2019, windows 10 64bit.
Related
When I compile the following code in Visual Studio 2019 I receive the following error code: code of the error output. It seems to be a LNK2028 error but I do not understand what is happening. Can someone explain why I receive this error?
Here is my code:
#include "MyForm.h"
#include <windows.h>
#include <windowsx.h>
#include <winuser.h>
#include <tchar.h>
using namespace System;
using namespace System::Windows::Forms;
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
HWND p = FindWindowEx(hwnd, NULL, _T("SHELLDLL_DefView"), false);
HWND* ret = (HWND*)lParam;
if (p) {
// Gets the WorkerW Window after the current one.
*ret = FindWindowEx(NULL, hwnd, _T("WorkerW"), NULL);
}
return true;
}
HWND get_wallpaper_window()
{
// Fetch the Progman window
HWND progman = FindWindow(_T("ProgMan"), NULL);
// Send 0x052C to Progman. This message directs Progman to spawn a
// WorkerW behind the desktop icons. If it is already there, nothing
// happens.
SendMessageTimeout(progman, 0x052C, 0, 0, SMTO_NORMAL, 1000, NULL);
// We enumerate all Windows, until we find one, that has the SHELLDLL_DefView
// as a child.
// If we found that window, we take its next sibling and assign it to workerw.
HWND wallpaper_hwnd = NULL;
EnumWindows(EnumWindowsProc, (LPARAM)&wallpaper_hwnd);
// Return the handle you're looking for.
return wallpaper_hwnd;
}
[STAThread]
void main(array<String^>^ args)
{
Application::EnableVisualStyles;
Application::SetCompatibleTextRenderingDefault(false);
DEM0::MyForm form;
Application::Run(% form);
}
As mentioned in this and this post it seems like the issue is due to a linker error which is caused by (I believe 8 function calls) that are depending on libraries implemented by the User32.dll located in C:\windows\system32 more info about this.
In this current project it seems like you are missing to include additional dependencies. You can include those by going to Project Properties -> Linker -> Input -> Additional Dependencies. Here you can choose to include the User32.dll file or choose the "Inherit from parent or project defaults" option. I believe this will solve your linker errors.
Preface: I'm fairly new to C++ and am just beginning to seriously program.
Post-Preface: I tried to post a link for the functions/pages I mention in this post, but Stack Overflow yelled at me because I don't have enough reputation to post more than two links.
I'm attempting to make a few simple GUIs in C++ with the Windows API using MinGW and the command line. I'm trying to change the window background, and one function which helps do this is the CreateSolidBrush function. This function requires the gdi32 library, but each time I try to compile/link to this library, I receive an error along the lines of "can't find that library, sucka".
Page1 and page2 provide useful information about MinGW's library functionality. Stack Overflow post # 5683058 and # 17031290 describe what I think are similar questions/problems to mine. I've searched far and wide for a simple and direct answer about how to link files/libraries from other directories (especially Windows libraries), but no luck in implementing the knowledge from these pages. Perhaps the answer is staring me right in the face, but my valiant efforts to "see the cat, draw the tiger" are in vain. It's possible that I'm entering the incorrect path/name (lib vs dll?), or maybe I'm completely overlooking something more fundamental (missing a header?). One command I've tried to use is
g++ -LC:\WINDOWS\System32 -lgdi32 gui.cpp
but this doesn't seem to work (note: source file named "gui.cpp").
Question 1: To start simple, what is the proper notation/command to link to individual header/source files which are not in the current directory?
Question 2: What is the proper notation/command to link to a library which is in the current directory?
Question 3: What is the proper notation/command to link to a library which is not in the current directory?
I realize these questions are sorta-kinda answered in a variety of ways on other pages, but they're often mingled with directions regarding Visual Studio, Eclipse, Code::Blocks, etc. and therefore unclear for the novices who forgo the luxuries of an IDE. I would appreciate a straightforward answer for your typical, run-of-the-mill noob. Many thanks in advance for any help or guidance.
I'll post my code, but I'm thinking only a couple of the first five lines are relevant:
#include <windows.h>
#include <string>
COLORREF desired_color = RGB(200,200,200);
HBRUSH hBrush = CreateSolidBrush(desired_color);
static char str_class_name[] = "MyClass";
static char str_titlebar[] = "My Window Title";
static int window_width = 300;
static int window_height = 300;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static HINSTANCE program_global_instance = NULL;
int WINAPI WinMain(HINSTANCE program_current_instance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
program_global_instance = program_current_instance;
WNDCLASSEX window_class;
HWND window_handle;
MSG window_message;
window_class.cbSize = sizeof(WNDCLASSEX); // size of struct; always set to size of WndClassEx
window_class.style = 0; // window style
window_class.lpfnWndProc = WndProc; // window callback procedure
window_class.cbClsExtra = 0; // extra memory to reserve for this class
window_class.cbWndExtra = 0; // extra memory to reserve per window
window_class.hInstance = program_global_instance; // handle for window instance
window_class.hIcon = LoadIcon(NULL, IDI_APPLICATION); // icon displayed when user presses ALT+TAB
window_class.hCursor = LoadCursor(NULL, IDC_ARROW); // cursor used in the program
window_class.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // brush used to set background color
window_class.lpszMenuName = NULL; // menu resource name
window_class.lpszClassName = str_class_name; // name with which to identify class
window_class.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // program icon shown in taskbar and top-left corner
if(!RegisterClassEx(&window_class)) {
MessageBox(0, "Error Registering Class!", "Error!", MB_ICONSTOP | MB_OK);
return 0;
}
window_handle = CreateWindowEx(
WS_EX_STATICEDGE, // dwExStyle: window style
str_class_name, // lpClassName: pointer to class name
str_titlebar, // lpWindowName: window titlebar
WS_OVERLAPPEDWINDOW, // dwStyle: window style
CW_USEDEFAULT, // x: horizontal starting position
CW_USEDEFAULT, // y: vertical starting position
window_width, // nWidth: window width
window_height, // nHeight: window height
NULL, // hWndParent: parent window handle (NULL for no parent)
NULL, // hMenu: menu handle (Null if not a child)
program_global_instance, // hInstance : current window instance
NULL // lpParam -Points to a value passed to the window through the CREATESTRUCT structure.
);
if (window_handle == NULL) {
MessageBox(0, "Error Creating Window!", "Error!", MB_ICONSTOP | MB_OK);
return 0;
}
ShowWindow(window_handle, nCmdShow);
UpdateWindow(window_handle);
while(GetMessage(&window_message, NULL, 0, 0)) {
TranslateMessage(&window_message);
DispatchMessage(&window_message);
}
return window_message.wParam;
}
// window_handle: window ID
// uMsg: window message
// wParam: additional message info; depends on uMsg value
// lParam: additional message info; depends on uMsg value
LRESULT CALLBACK WndProc(
HWND window_handle,
UINT Message,
WPARAM wParam,
LPARAM lParam
) {
switch(Message) {
case WM_CLOSE:
DestroyWindow(window_handle);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(window_handle, Message, wParam, lParam);
}
return 0;
}
Question 1: To start simple, what is the proper notation/command to link to individual header/source files which are not in the current directory?
That's not linking, that's compiling/including (unless you compile those source files to object files, first).
So:
gcc {other options} -o gui.exe gui.cpp /path/to/source_file_one.cpp /path/to/source_file_n.cpp
or, compile the others first:
gcc {other options} -c -o source_file_one.o /path/to/source_file_one.cpp
gcc {other options} -c -o source_file_n.o /path/to/source_file_n.cpp
gcc {other options} -o gui.exe source_file_n.o source_file_one.o gui.cpp
-c tells gcc not to try and link things together, as this is done in the last step.
{other options} can contain -I{include dirs} to inform gcc where to look when you #include something.
Question 2: What is the proper notation/command to link to a library which is in the current directory?
See 3; -L. should work.
Question 3: What is the proper notation/command to link to a library which is not in the current directory?
You're doing it right, so far: -L tells gcc about paths to look into for libraries, and -l{libname} links against a library.
I want to use Chromium Embeded Framework in an mfc dll. Therefore I created a mfc test exe and tested a minimalistic implementation of cef (which worked). My next step was to do exactly the same thing with a test mfc dll. The code is almost the same but the cef browser window won't come up.
The application hangs in CefInitialize() and won't return. After debugging, I found that it gets stuck in a WaitForSingleObject/WaitForMultipleObject windows api.
BOOL CMyBrowserDllApp::InitInstance()
{
HINSTANCE hInstance = GetModuleHandle(NULL);
CefMainArgs main_args(hInstance);
CefSettings settings;
settings.no_sandbox = true;
settings.multi_threaded_message_loop = true;
// Execute the secondary process, if any.
int exit_code = CefExecuteProcess(main_args, cefApplication.get(), NULL);
if (exit_code >= 0)
return exit_code;
if(!CefInitialize(main_args, settings, cefApplication.get(), NULL))
{
OutputDebugStringA("Error: CefInitialize failed");
}else
OutputDebugStringA("Info: CefInitialize succeeded");
CWinApp::InitInstance();
return TRUE;
}
Same implementation works if used in a mfc exe directly.
GetModuleHandle returns the base for the exe not the dll. You might want to stash the dll base from:
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void *reserved)
This is with a vs2010 MFC Dialog Application. Besides the below code I've also tried including the following libs, ehstorguids.lib Uuid.Lib. The end result I'm aiming for is to kill the windows 8 Charms Bar. What am I missing to cause this undeclared identifier.
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <propsys.h>
#include <propkey.h>
using namespace std;
HRESULT SetTouchDisableProperty(HWND hwnd, BOOL fDisableTouch)
{
IPropertyStore* pPropStore;
HRESULT hrReturnValue = SHGetPropertyStoreForWindow(hwnd, IID_PPV_ARGS(&pPropStore));
if (SUCCEEDED(hrReturnValue))
{
PROPVARIANT var;
var.vt = VT_BOOL;
var.boolVal = fDisableTouch ? VARIANT_TRUE : VARIANT_FALSE;
hrReturnValue = pPropStore->SetValue(PKEY_EdgeGesture_DisableTouchWhenFullscreen, var);
pPropStore->Release();
}
return hrReturnValue;
}
BOOL CALLBACK MyEnumProc(HWND hWnd, LPARAM lParam)
{
TCHAR title[500];
ZeroMemory(title, sizeof(title));
GetWindowText(hWnd, title, sizeof(title)/sizeof(title[0]));
if (!_tcscmp(title, _T("helloworld")))
{
SetTouchDisableProperty(hWnd,true);
}
return TRUE;
}
void mymfcdialog::ObBnClickedOk()
{
EnumWindows(MyEnumProc, 0);
}
Windows 8 SDK is required for declaration even though VS was not complaining about the libs which are required per MSDN which where included.
This method of hiding/disabling Charms only works with desktop apps when they are full screen and have a title bar. So in my case, this would not working. Killing the 'explorer' process when no charms bar/gestures are desired and then relaunching the explorer process is the only option. If MS reads this, you really should look around, this should not be required to hide/disable Charms/Gestures. But then again, look at windows at Windows 8...I mean 8.1....Still haven't got it right.
Your original code doesn't look wrong to me. In fact, there are working examples of this online.
The best I found was https://github.com/Kuqd/DisableCharmBar, which demonstrates a working example.
The limitation regarding the window title bar, which you mention in your own answer, might be related to your GetWindowText checks. The example from Kuqd works without having a title bar.
I'm using this in my code, i hope it can help you:
static Guid DISABLE_TOUCH_SCREEN = new Guid("32CE38B2-2C9A-41B1-9BC5-B3784394AA44"), // PKEY_EdgeGesture_DisableTouchWhenFullscreen
IID_PROPERTY_STORE = new Guid("886d8eeb-8cf2-4446-8d02-cdba1dbdcf99"); // PropertyStore
static short VT_BOOL = 11;
private const int GC_ALLGESTURES = 0x00000001;
public static void DisableEdgeGestures(IntPtr hwnd)
{
win32.IPropertyStore pPropStore = null;
int hr = 0;
hr = win32.SHGetPropertyStoreForWindow(hwnd, ref IID_PROPERTY_STORE, out pPropStore);
if (hr == 0)
{
win32.PropertyKey propKey = new win32.PropertyKey();
propKey.fmtid = DISABLE_TOUCH_SCREEN;
propKey.pid = 2;
win32.PropVariant var = new win32.PropVariant();
var.vt = VT_BOOL;
var.boolVal = true;
pPropStore.SetValue(ref propKey, ref var);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(pPropStore);
}
}
I'm new to c++ programming and am trying to make a small reporting tool that will detect when the mouse and keyboard haven't been touched over a period of time.
I have been searching for some example mouse hook code and found this
#define _WIN32_WINNT 0x0400
#pragma comment( lib, "user32.lib" )
#include <windows.h>
#include <stdio.h>
HHOOK hMouseHook;
__declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
{
MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
if (pMouseStruct != NULL)
printf("Mouse position X = %d Mouse Position Y = %d\n", pMouseStruct->pt.x,pMouseStruct->pt.y);
return CallNextHookEx(hMouseHook,
nCode,wParam,lParam);
}
void MessageLoop()
{
MSG message;
while (GetMessage(&message,NULL,0,0)) {
TranslateMessage( &message );
DispatchMessage( &message );
}
}
DWORD WINAPI MyMouseLogger(LPVOID lpParm)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
if (!hInstance) hInstance = LoadLibrary((LPCSTR) lpParm);
if (!hInstance) return 1;
hMouseHook = SetWindowsHookEx (
WH_MOUSE_LL,
(HOOKPROC) KeyboardEvent,
hInstance,
NULL
);
MessageLoop();
UnhookWindowsHookEx(hMouseHook);
return 0;
}
int main(int argc, char** argv)
{
HANDLE hThread;
DWORD dwThread;
hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)
MyMouseLogger, (LPVOID) argv[0], NULL, &dwThread);
if (hThread)
return WaitForSingleObject(hThread,INFINITE);
else return 1;
}
I compiled and ran this as a test which seems to do what I wanted. After that I started testing a couple of things. compiled on c++ and g++ for the hell of it. removed the pMouseStruct and tested "if(lParam)" instead. Everything seemed to be behaving and it was getting late so I decided to return to this the next day.
When I booted in to windows today, I was unable to move the mouse or input anything with the keyboard. It seems input inside windows (normal and safe mode) is not working anymore. Any ideas how to fix this?
Using Windows 7 64bit.
So far I have tried the following:
- reversed a recent overclocking tweak.
- returned the code to the way it was before i played with it, recompiled, and ran.
- Googling.
- chkdsk /f on the system drive.
- system restore (doesn't like raid)
- copied user32.dll off a friend.
Plz forgive my ignorance :-)
I ended up reinstalling windows 7 and soon ran into more strange problems. I have since discovered that standby was corrupting various things and have stopped using it. Now I'm running a fresh install of windows 7 that has never been put to sleep. Still don't have any bizarre problems to this day :-)