Dark Bit Factory & Gravity
PROGRAMMING => C / C++ /C# => Topic started by: Paul on December 26, 2007
-
Hi
I got a Logitech G15 keyboard for Christmas and now I'm trying to program it. I'ts got a monochrome graphical LCD screen.
I've managed to get a simple program going in c++, but since i prefer Blitz3D I would like to create a dll with some simple functions.
This works
lgLcdOpenContext InnitLCD(LPCSTR name)
{
DWORD res;
//// initialize the library
res=lgLcdInit();//res
HandleError(res, _T("lgLcdInit"));
//// connect to LCDMon
// set up connection context
lgLcdConnectContext connectContext;
ZeroMemory(&connectContext, sizeof(connectContext));
connectContext.appFriendlyName = _T(name);//"simple sample");
connectContext.isAutostartable = false;
connectContext.isPersistent = false;
// we don't have a configuration screen
connectContext.onConfigure.configCallback = NULL;
connectContext.onConfigure.configContext = NULL;
// the "connection" member will be returned upon return
connectContext.connection = LGLCD_INVALID_CONNECTION;
// and connect
res=lgLcdConnect(&connectContext);//RES
HandleError(res, _T("lgLcdConnect"));
// now we are connected (and have a connection handle returned),
// let's enumerate an LCD (the first one, index = 0)
lgLcdDeviceDesc deviceDescription;
res=lgLcdEnumerate(connectContext.connection, 0, &deviceDescription);//res
HandleError(res, _T("lgLcdEnumerate"));
// at this point, we have an LCD
//_tprintf(_T("Found an LCD with %dx%d pixels, %d bits per pixel and %d soft buttons\n"),
// deviceDescription.Width, deviceDescription.Height, deviceDescription.Bpp,
// deviceDescription.NumSoftButtons);
// open it
lgLcdOpenContext openContext;
ZeroMemory(&openContext, sizeof(openContext));
openContext.connection = connectContext.connection;
openContext.index = 0;
// we have no softbutton notification callback
openContext.onSoftbuttonsChanged.softbuttonsChangedCallback = NULL;
openContext.onSoftbuttonsChanged.softbuttonsChangedContext = NULL;
// the "device" member will be returned upon return
openContext.device = LGLCD_INVALID_DEVICE;
res=lgLcdOpen(&openContext);//res
HandleError(res, _T("lgLcdEnumerate"));
return openContext;
}
But since Blitz cant get the returned class i need to send it to my function, and let c++ edit the content
This code here doesn't work though
void InnitLCD(LPCSTR name,lgLcdOpenContext openContext)
{
DWORD res;
//// initialize the library
res=lgLcdInit();//res
HandleError(res, _T("lgLcdInit"));
//// connect to LCDMon
// set up connection context
lgLcdConnectContext connectContext;
ZeroMemory(&connectContext, sizeof(connectContext));
connectContext.appFriendlyName = _T(name);//"simple sample");
connectContext.isAutostartable = TRUE;
connectContext.isPersistent = TRUE;
// we don't have a configuration screen
connectContext.onConfigure.configCallback = NULL;
connectContext.onConfigure.configContext = NULL;
// the "connection" member will be returned upon return
connectContext.connection = LGLCD_INVALID_CONNECTION;
// and connect
res=lgLcdConnect(&connectContext);//RES
HandleError(res, _T("lgLcdConnect"));
// now we are connected (and have a connection handle returned),
// let's enumerate an LCD (the first one, index = 0)
lgLcdDeviceDesc deviceDescription;
res=lgLcdEnumerate(connectContext.connection, 0, &deviceDescription);//res
HandleError(res, _T("lgLcdEnumerate"));
// at this point, we have an LCD
//_tprintf(_T("Found an LCD with %dx%d pixels, %d bits per pixel and %d soft buttons\n"),
// deviceDescription.Width, deviceDescription.Height, deviceDescription.Bpp,
// deviceDescription.NumSoftButtons);
// open it
// lgLcdOpenContext openContext;
ZeroMemory(&openContext, sizeof(openContext));
openContext.connection = connectContext.connection;
openContext.index = 0;
// we have no softbutton notification callback
openContext.onSoftbuttonsChanged.softbuttonsChangedCallback = NULL;
openContext.onSoftbuttonsChanged.softbuttonsChangedContext = NULL;
// the "device" member will be returned upon return
openContext.device = LGLCD_INVALID_DEVICE;
res=lgLcdOpen(&openContext);//res
HandleError(res, _T("lgLcdEnumerate"));
//return openContext;
}
So how would i edit the fisrts peice of code so i can call it like this:
lgLcdOpenContext openContext;
memset(&openContext,0,sizeof(openContext));
InnitLCD(_T("NAME"),openContext);
-
You need to look up the definition of lgLcdOpenContext and work out how big it needs to be. Then create a bank of that size and use the bank handle as the address to pass
openContext = CreateBank(...)
...set all entries in the bank to 0
InnitLCD("NAME",openContext);
...now you need to use Peek to grab the returned values back out of the bank.
This all relies on the undocumented fact that CreateBank returns a pointer to the memory!
Jim
-
Ty jim, but i realised that i posted the wrong code, so please have a look again :S
Ill try to clarify a little bit more aswell:
I've got the function lgLcdOpenContext InnitLCD(LPCSTR name) working, but since blitz cant take returned banks, arrays or types i need a function like this:
void InnitLCD(LPCSTR name,lgLcdOpenContext openContext) where i can send a bank to my dll.
The problem is that i don't seem to get the conversion right.
-
I see. I think you need to make it
void InnitLCD(LPCSTR name, lgLcdOpenContext *openContext)
So it's taking a pointer to the context.
Jim
-
A german guy made a dll that can be used with Blitz3D, so you might want to check that one out.
http://www.blitzforum.de/forum/viewtopic.php?t=26391 (http://www.blitzforum.de/forum/viewtopic.php?t=26391)
-
Thanks got it to work :carrot:
This is my first real attempt to do something in cpp.
-
And you're using it to program the lcd display on your keyboard? :)
Hehe, I love goofy stuff like this have some good Karma.
-
Ty, one more question:
To get the volume wheel input without changing the volume (get the input to my program and stopping windows from changing the volume) I'm setting a low level keyboard hook. the problem is that I've only got it to work in a Windows application and not in my dll. Is there something specific that i need to include to the dll?
here is my function:
static HHOOK hHook = NULL;
LRESULT CALLBACK HookProc (int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
LPKBDLLHOOKSTRUCT kbhs = (LPKBDLLHOOKSTRUCT)lParam;
if ((kbhs->vkCode == VK_VOLUME_DOWN || kbhs->vkCode == VK_VOLUME_UP) && wParam == WM_KEYDOWN)
{
MessageBoxA(0,"Someone rolled the volume wheel","HI",0);
return 1;
}
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
-
How is the code that sets up the hook called? Can you check you are getting any hooked messages at all? Maybe the keyboard driver is in the hook chain before yours? Just some ideas.
Jim
-
I'm running it like this:
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)HookProc, 0, 0);
And freeing it like this:
UnhookWindowsHookEx(hHook);
wehre hHook is a static
and as i said, it works in the "Windows Project", but not in the "Dll". I'm using M$ vc++ 2005 express edition, maybe has something to do with that.
My Hook procedure doesn't seem to get called at all, as if there where some kind of block.
edit:
forgot to mention that i get a few errors if i just copy the functions to the dll:
1>.\dllmain.cpp(87) : error C2065: 'WH_KEYBOARD_LL' : undeclared identifier
1>.\dllmain.cpp(208) : error C2065: 'LPKBDLLHOOKSTRUCT' : undeclared identifier
1>.\dllmain.cpp(209) : error C2065: 'VK_VOLUME_DOWN' : undeclared identifier
1>.\dllmain.cpp(209) : error C2065: 'VK_VOLUME_UP' : undeclared identifier
and various others because of these, if i define all these it compies but windows doesn't seem to call my hook procedure.
-
Added this before "include <windows.h>", and it worked
#pragma once
// Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef WINVER // Allow use of features specific to Windows XP or later.
#define WINVER 0x0501 // Change this to the appropriate value to target other versions of Windows.
#endif
#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.
#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
#endif
#ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later.
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
#endif
#ifndef _WIN32_IE // Allow use of features specific to IE 6.0 or later.
#define _WIN32_IE 0x0600 // Change this to the appropriate value to target other versions of IE.
#endif
#define WIN32_LEAN_AND_MEAN
but it isn't fast, probably not meant to have a volume wheel, only keys
If i scroll from 0-100 fast it continues to over 230 :D but i can live with that if there isn't an easy solution.
-
I suspect you only need _WINVER set. And I also suspect you get the raw events, hence the 'random' values.
Jim
-
Hi again...
Dont know if i should have started a new topic but I'll try it like here.
I'm trying to get the hook thing to work in freebasic, but something is wrong and i can't find it.
Windows doesn't seem to run my hookproc at all. one of my attempts to solve the problem was to dump in all then defines that i could find, but it didn't help.
'#pragma once
' Modify the following defines if you have to target a platform prior to the ones specified below.
' Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef WINVER ' Allow use of features specific to Windows XP or later.
#define WINVER 0x0501 ' Change this to the appropriate value to target other versions of Windows.
#endif
#ifndef _WIN32_WINNT ' Allow use of features specific to Windows XP or later.
#define _WIN32_WINNT 0x0501 ' Change this to the appropriate value to target other versions of Windows.
#endif
#ifndef _WIN32_WINDOWS ' Allow use of features specific to Windows 98 or later.
#define _WIN32_WINDOWS 0x0410 ' Change this to the appropriate value to target Windows Me or later.
#endif
#ifndef _WIN32_IE ' Allow use of features specific to IE 6.0 or later.
#define _WIN32_IE 0x0600 ' Change this to the appropriate value to target other versions of IE.
#endif
#define WIN32_LEAN_AND_MEAN
#define WIN_INCLUDEALL
#include "windows.bi"
option explicit
option static
DECLARE function HookProc (nCode as integer, wParam as WPARAM, lParam as LPARAM) as LRESULT
dim shared hHook as long 'Handle to the keyboard proc
hHook=SetWindowsHookEx(WH_KEYBOARD_LL,@hookproc,0,0)
if hHook=0 then print "failed to create hook"
dim oks as integer 'the state escape was in when the program was started
print "Press escape to end"
oks=getkeystate(vk_escape)
while getkeystate(vk_escape)=oks
wend
UnhookWindowsHookEx(@hookproc) 'free the hook
function HookProc (nCode as integer, wParam as WPARAM, lParam as LPARAM) as LRESULT
if (nCode = HC_ACTION) then
'original code
'LPKBDLLHOOKSTRUCT kbhs = (LPKBDLLHOOKSTRUCT)lParam
'FB converions(I hope)
dim kbhs as LPKBDLLHOOKSTRUCT' = lParam
'kbhs=cast(LPKBDLLHOOKSTRUCT,lParam)
kbhs=lParam
if ((kbhs->vkCode = VK_VOLUME_DOWN or kbhs->vkCode = VK_VOLUME_UP) and wParam = WM_KEYDOWN) then
print "hi"
return 1
endif
endif
print "A"
return CallNextHookEx(hHook, nCode, wParam, lParam)
end function
[code][/code]