[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C++ Combobox/edit text to GetAsyncKeyState/Sleep

Posted on 2015-01-17
14
Medium Priority
?
280 Views
Last Modified: 2015-01-17
Hello,

I'm trying to get the text of a combobox and edits to pass it to GetAsyncKeyState and Sleep functions.

I have a button "Apply"

case IDC_BUTTON1:{
			char key[MAX_PATH];
			char s[MAX_PATH];
			char b[MAX_PATH];
			char h[MAX_PATH];
			GetDlgItemText(hWnd, IDC_COMBO1, key, sizeof(key));
			GetDlgItemText(hWnd, IDC_EDIT1, s, sizeof(s));
			GetDlgItemText(hWnd, IDC_EDIT2, b, sizeof(b));
			GetDlgItemText(hWnd, IDC_EDIT3, h, sizeof(h));
			//strings
			M1::Threads::Inst().key = std::string(key);
			M1::Threads::Inst().start = std::string(s);
			M1::Threads::Inst().between = std::string(b);
			M1::Threads::Inst().holding = std::string(h);
			break;
		}

Open in new window


When i hit this button the program crash; idk how to debug because it's inside a dll.

Passing to GetAsyncKeyState and Sleep:

GetAsyncKeyState((int)&M1::Threads::Inst().key)

Sleep((DWORD)&M1::Threads::Inst().start);
Sleep((DWORD)&M1::Threads::Inst().holding);
Sleep((DWORD)&M1::Threads::Inst().between);

Open in new window


This is how i set text to combobox:

case WM_INITDIALOG:{
		SetDlgItemText(hWnd, IDC_EDIT1, "0"); //start
		SetDlgItemText(hWnd, IDC_EDIT2, "0"); //btween
		SetDlgItemText(hWnd, IDC_EDIT3, "25"); //hold
	
		std::string keys[8] = { "VK_XBUTTON1", "VK_XBUTTON2", "VK_CONTROL", "VK_SPACE", "0x45", "0x46", "0x47", "NO KEY" };
		for (int i = 0; i < 8; i++){
			AddItemComboBox(GetDlgItem(hWnd, IDC_COMBO1), (LPARAM)keys[i].c_str());
		}
		SendMessage(GetDlgItem(hWnd, IDC_COMBO1), CB_SETCURSEL, 5, NULL);
		break;
	}

Open in new window


Ty.
0
Comment
Question by:Júlio
  • 10
  • 4
14 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 40555213
What exactly is the call stack when the crash  occurs?
0
 

Author Comment

by:Júlio
ID: 40555222
This dll is injected. idk.

After inject i create a window from dll, the main application (injector) do not import any functions.

Before you ask, it's a macro project using injected dlls. And it's personal.
0
 
LVL 86

Expert Comment

by:jkr
ID: 40555227
Well, injected or not, you can just attach Visual Studio to the process you are injecting the DLL into (via "Debug|Attach to Process") and check the crash there, just like with any regular application you're debugging.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Júlio
ID: 40555229
I'm trying, but the target application is .net 4.0, and my VS 2013 says that is 2.0/3.0. I never did this before(debug another application).
0
 

Author Comment

by:Júlio
ID: 40555234
Hum, what i'm doing.
I open the target application.
I inject.
Now i open VS, attach to my window that is in the target.
I click "Apply" button, the app crash.

I stack don't show nothing, only got this:

has exited with code -1073741819 (0xc0000005) 'Access violation'.
0
 
LVL 86

Expert Comment

by:jkr
ID: 40555239
I think we found the problem: If you want to inject DLLs into other processes, they either have to be native or or the Framework versions have to be compatible. 4.0 and 2.0/3.0 definitely aren't. See http://msdn.microsoft.com/en-us/library/ff602939%28v=vs.110%29.aspx ("Version Compatibility in the .NET Framework")
0
 

Author Comment

by:Júlio
ID: 40555265
Ok, i checked it, and my dll(win32) was created using .net 4.5. All my projects i set to 4.5 (it's default here).

But if i try to find this info in *.vcsproj file i can't.

I build it as win32 dll.
And btw, if this was the problem, the dll it self would not show the window, because i create a thread in DLL_ATTACH and it should crash here too.

BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
	if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
		inj_hModule = hModule;
		CreateThread(0, NULL, ThreadProc, (LPVOID)"Welcome", NULL, NULL);
	}
	return TRUE;
}

Open in new window

0
 

Author Comment

by:Júlio
ID: 40555289
I did a application test.

Attach it after inject.

When it crash the problem is here:

class Threads{
	public:
		static Threads& Inst(){
			static Threads thread; << this line
			return thread;
		}

Open in new window

           
The thread 0x7168 has exited with code 0 (0x0).
First-chance exception at 0x67571548 (dll.dll) in PE_Sections.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x67571548 (dll.dll) in PE_Sections.exe: 0xC0000005: Access violation reading location 0x00000000.

Singleton problem?

I will use it in threads, so pretty sure it gonna give me more problems. I need your help to fix it.

=/ didn't know that is possible to see all my code like this.
First time that i have problem like this. ihave others classes with others singlestons and everything is working fine.
call-stack.PNG
At the end, the problem is not the button. =_(
0
 

Author Comment

by:Júlio
ID: 40555352
I fixed it, now the problem is the values.

Fixed the class/singleton:
* i changed all my singletons to this type.

	class Threads{
	private:
		Threads() {}

	public:
		static Threads& Inst(){
			static Threads * Inst = new Threads();
			return *Inst;
		}

Open in new window


Now we are back to original question.


How to receive the values of a combobox to a GetAsyncKeyState and edit to Sleep functions.

I'm like doing like in original question:

GetAsyncKeyState((int)&M1::Threads::Inst().key)

Sleep((DWORD)&M1::Threads::Inst().start);
Sleep((DWORD)&M1::Threads::Inst().holding);
Sleep((DWORD)&M1::Threads::Inst().between);

Open in new window


@jkr where are you xD help me Master.
0
 

Author Comment

by:Júlio
ID: 40555511
I was reading msdn, and probally my problem is here, about GetAsyncKeyState, idk.

The return value is zero for the following cases:
The current desktop is not the active desktop
The foreground thread belongs to another process and the desktop does not allow the hook or the journal record.

Let's say i want to macro in another window and use this key to trigger the macro, i can't ( i'm understanding this).
Solution: hook?

For Sleep, atoi is working.
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 40555594
A hook is probably the easier way if you want to get the keystrokes. In poarticular, a WH_KEYBOARD hook. Check out the samples at http://msdn.microsoft.com/en-us/library/windows/desktop/ms644960%28v=vs.85%29.aspx ("Using Hooks"). If you need to stick with .NET, you might find this article http://www.codeproject.com/Articles/19858/Global-Windows-Hooks ("Global Windows Hooks") interesting.

BTW, sorry for the delay, but it's WE.
0
 

Author Comment

by:Júlio
ID: 40555610
HUm, i was testing like this:

GetAsyncKeyState(VK_XBUTTON1), and it's working fine.

The problem is convert the text from combobox to use.

This do not work too:"

(SHORT)(M1::Threads::Inst().key.c_str());

Open in new window


or

(int)&M1::Threads::Inst().key.c_str();

Open in new window


Hook is to aggressive only to do this i think.
And i can't figure out how to convert the text from combobox to use here.

I'm using this array to add text to combobox.

std::string keys[7] = { "VK_XBUTTON1", "VK_XBUTTON2", "VK_CONTROL", "VK_SPACE", "0x45", "0x46", "0x47" };

Open in new window


HUm found this one: VkKeyScanEx, but i can't accept mouse buttons. =p
0
 

Author Comment

by:Júlio
ID: 40555726
I'm using the combobox item index to solve this:

M1::Threads::Inst().item = SendMessage(GetDlgItem(hWnd, IDC_COMBO1), CB_GETCURSEL, 0, 0);

int vkeys[7] = {
		VK_XBUTTON1,
		VK_XBUTTON2,
		VK_CONTROL,
		VK_SPACE,
		0x45,
		0x46,
		0x47,
	};

GetAsyncKeyState(vkeys[M1::Threads::Inst().item])

Open in new window


Ty for help me! You are a nice guy!
0
 

Author Closing Comment

by:Júlio
ID: 40555727
He is very good!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The article covers five tools all IT professionals should know about, as they up productivity by a great deal!
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Six Sigma Control Plans

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question