|
For global hooks you need the hook proc in a DLL. Now assuming we want to do this from .NET, we'll have a problem. At least I am having problems. .NET DLLs dont seem to be real DLLs as far as I am concerned
I am wondering what to pass as the HINSTANCE parameter for SetWindowsHookEx!
Regards,
Nish
Author of the romantic comedy
Summer Love and Some more Cricket [New Win]
Review by Shog9
Click here for review[NW]
|
|
|
|
|
Hey Nish,
I was just looking into this for my painting woes. I have not tried it yet, and I'm not sure if it applies to you but look at section 44.6 at this link:
http://www.syncfusion.com/FAQ/WinForms/FAQ_c70c.asp
|
|
|
|
|
|
Hi Nish
Dunno if this will help?
Marshal.GetHINSTANCE() returns IntPtr to HINSTANCE.
HTH
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
|
|
Did someone get a successfull way to register hotkeys in C# ?
eg : CONTROL + SHIFT + J or whatever else which works whoever has the focus.
[DllImport("user32.dll")] private static extern bool RegisterHotKey(long hWnd, int id, int fsModifiers, int vk);
// MOD_CONTROL + MOD_ALT = 6
bool b = RegisterHotKey(GetForegroundWindow(),100,6, 'j'); /*0x4A*/ or 'j' doesn't work.
b is always false...No way to register anything.
Please help, my program is useless whithout hotkeys. Thanks.
Nd.
PS :
MOD_ALT 1
MOD_CONTROL 2
MOD_SHIFT 4
MOD_WIN 8
|
|
|
|
|
Anonymous wrote:
[DllImport("user32.dll")] private static extern bool RegisterHotKey(long hWnd, int id, int fsModifiers, int vk);
The returned value of the function is a C++ BOOL iow in C# an int, 0 = false all else = true;
Maybe this helps
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
Thanks Leppie, I didn't know that. It wasn't working because I wasn't doing the registration with the right handle ! I was using GetForegroundWindow during form init, and at this moment, the handle is pointing to Visual Studio and not the launched application...
Nd.
|
|
|
|
|
This works for me
First the definitions:
[DllImport("user32.dll", SetLastError=true)]
public static extern bool RegisterHotKey(
IntPtr hWnd,
int id,
KeyModifiers fsModifiers,
Keys vk
);
[DllImport("user32.dll", SetLastError=true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd,
int id
);
[Flags()]
public enum KeyModifiers
{
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
} Now a sample use of the code:
In Form load:
bool success = RegisterHotKey(Handle, 100,
KeyModifiers.Control | KeyModifiers.Shift, Keys.J);
System.Diagnostics.Trace.WriteLine(
"Success = " + success.ToString()); In Form closed or closing:
UnregisterHotKey(Handle, 100); And finally override the WndProc so the message can be handled.
protected override void WndProc( ref Message m )
{
const int WM_HOTKEY = 0x0312;
switch(m.Msg)
{
case WM_HOTKEY:
MessageBox.Show("Hotkey pressed");
break;
}
base.WndProc(ref m );
} HTH,
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
Thanks for your answer and the needed time. I replace all my code by yours and it still didn't work...That give me the idea to search for something else and I got it. I was using GetActiveWindow in order to get the handle of the current program...I know, it's not the best way...I will search for a better one later, time is running. During the init of the form, the GetActiveWindow returns the handle of Visual Studio and not the one of my program. So I was trying to register hotkeys for Visual Studio and it wasn't working.
Well, thanks to you it's working fine now, I was gonna give up (too much time spent on it)
My program should be ready for tomorrow with minimal features.
Nd.
|
|
|
|
|
If you want to get the HWND for your Form use the Handle property of the Form.
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
Interesting
Just a question James...
In the Interop Marshalling section (Platform Invoke Data Types) in MSDN it states:
BOOL as Unmanaged type in Wtypes.h = long in Unmanaged C++ = System.Int32 as Managed type.
Looking at your code, is it possible to allways use bool instead of int as long as the returned value does not hold somethin like an error code?
Also, in alot of examples I see them using int for a DWORD which really translates to uint . OK, I understand it will work, but only if int => 0 . Why not use uint in the first place?
This is all getting a bit overcomplicated to me.
I think probably the best is to keep it simple. Try the logical way, if fail, do the way according to Platform Invoke Data Types Table.
Suggestions?
Cheers
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
As you've found out, all that is important is that the size be the same. However some things will get marshaled back and forth correctly.
The conversion between BOOL and LONG to bool will marshal correctly so that the bool will have a true/false value.
Using unsigned types in .NET isn't really discouraged but doing so makes the code non-CLS compliant so that code may not be portable to other languages or even used by them should they not support the same unsigned types. Thats my understanding anyway
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
Ahh, thx James. Didnt know about unsigned types being non CLS compliant.
re BOOL, i just made a small function to take care of it , but with this input i'll try converting some of them as see the results.
The only remaining worry is what happens when DWORD return a value bigger than Int32.MaxValue? Not that it has happened yet, but it could.
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
leppie wrote:
The only remaining worry is what happens when DWORD return a value bigger than Int32.MaxValue? Not that it has happened yet, but it could.
Unfortunately thats just something you have to know ahead of time and it depends on how the value is used.
If the DWORD contains something larger than Int32.MaxValue the return result will be negative as the LSB or MSB will be set (I never can remember how it should be ordered )
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
Thx again James
James T. Johnson wrote:
If the DWORD contains something larger than Int32.MaxValue the return result will be negative as the LSB or MSB will be set (I never can remember how it should be ordered )
According to MSDN an overflow exception will occur unless the block is marked unchecked. In which case the MSB/LSB (hell i dont know either, will wait till next year when I'm doing bitwise operations more regularly) will be set.
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
leppie wrote:
According to MSDN an overflow exception will occur unless the block is marked unchecked.
I don't see this case happening, because when the data is being marshalled the framework doesn't know the original context of the value (signed or unsigned it is still a 4-byte block of memory). In this case we are telling it to be interpreted as a signed integer so it should interpret any value with the MSB or LSB set as being negative (assuming 2's complement use of the bits). The only cases where the MSB/LSB is set are when you have values larger than Int32.MaxValue.
There may be some attributes that could be applied to make the framework aware of such occurances, but I'm not aware of them off the top of my head.
Now when you add 2 ints together in managed code the framework does know the context of those values so it can decide whether or not the result went over the max value.
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
Thx James, you dont seem to overlook anything. How do u do it? Of course the the unmanaged function has no clue to what it is returning its returned value to.
And I continue to slap myself repeatedly.....
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
leppie wrote:
Thx James, you dont seem to overlook anything. How do u do it?
I have no real social life and I've worked with .NET since Beta 1; chances are that I've already had to think a lot of issues through at one time or another
Other times something just comes to me that seems to fit and makes sense when I think it through; such was the case here when returning values from unmanaged functions. In this case it was my 10 week long assembly course I took my first year of uni where we spent about 2 weeks on how the processor stores numbers (bitwise).
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
How can I do Auto Tabbing on my Controls? Example on my Form I have two Text Box, reaching a specific length in first Text Box the focus automatically goes into the next Text Box.
Anybody HELP!
|
|
|
|
|
Easily
register keypress event, check the length of the textbox1's text and do textbox2.Select().
Sorry im a bit too lazy to write out the code
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
For an app that may or may not be run in the console (ie it can be run with a dlg interface or at the cmd prompt) how could one determine whether the app is running in the console or not in the entry point?
|
|
|
|
|
Nevermind... I didn't realize it was an either or situation. Really miss the allocconsole. Maybe we'll just have to import. That's disappointing.
|
|
|
|
|
Hi all again,
What type of resource can i embed into an assembly to use at runtime? Is it possible to embed (embedding works) a Win32 dll and use that dll directly from the embedded resources?
Also , how do you view what resources are in an assembly?
Any help will be appreciated.
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|