Tech Support > Microsoft Windows > Development Resources > Help how to create a window in DLL.
Help how to create a window in DLL.
Posted by Tuy Solang on September 20th, 2004


Hi,

Is there any way to create a Window inside of a DLL?

Thanks.

Posted by r_z_aret@pen_fact.com on September 20th, 2004


On Mon, 20 Sep 2004 15:47:06 GMT, Tuy Solang <solang@verizon.net>
wrote:

If you're using functions that explicitly reference resources, then
you will need an HINSTANCE for the DLL, rather than for the
executable. Otherwise, I can't think of any differences between window
creation code written for a DLL and an EXE. The CreateWindow function
works the same. I suspect I'm missing something in your question; if
so, then more info from you about your problem and why you expect a
difference might help me.


-----------------------------------------
To reply to me, remove the underscores (_) from my email address (and please indicate which newsgroup and message).

Robert E. Zaret, eMVP
PenFact, Inc.
500 Harrison Ave., Suite 3R
Boston, MA 02118
www.penfact.com

Posted by Tuy Solang on September 20th, 2004



I am writting a keyboard DLL and a window to display the keymap.
Both of them are working, except one thing that prevent the
keymap from updating since it loose it focus. Suppose a user
is woring on MS WordPad and change the font. Once he/she type
a key, the keyboard dll intecept the key stroke and ask the
font the user is selected. I am using

fhWnd=GetFocus();
hFont = (HFONT)SendMessage(fhWnd, WM_GETFONT, 0, 0);

Since MS WordPad is the focus window, the keyboard DLL can not
send any message to keymap window. I'd tried to use dllimport,
but I fail to understand how to use it. So, I've thought it
might be easier just to create a window inside the DLL program
space. That way the DLL can call any function to draw the keymap.

Thanks.


r_z_aret@pen_fact.com writes:

Posted by r_z_aret@pen_fact.com on September 21st, 2004


On Mon, 20 Sep 2004 20:08:09 GMT, Tuy Solang <solang@verizon.net>
wrote:

How is the keymap created (what functions and/or resources), and by
what process (the DLL, some other DLL, and executable, ...)?


Once the DLL gets a handle for the keymap window, the DLL can use that
handle to send messages without any concern about focus. I'll have
better ideas about how the DLL can get the handle once you give more
details about how the keymap window is created.


-----------------------------------------
To reply to me, remove the underscores (_) from my email address (and please indicate which newsgroup and message).

Robert E. Zaret, eMVP
PenFact, Inc.
500 Harrison Ave., Suite 3R
Boston, MA 02118
www.penfact.com

Posted by Tuy Solang on September 21st, 2004


Thanks for reply.
Here is how the program and the DLL are created :

1. a.dll with a few export functions.

2. b.exe with a.dll static linked;
b.exe didn't receive WM_CHAR from KeyboardProc at all.


a.dll
HWND my_hWnd=NULL,fhWnd;
char font_face[128];
int page_number=0;
unsigned char char_key[4][2][100];
BOOLEAN shift_flag=FALSE;

void init_char_key(void)
{
char_key[0][0][0]=' ';
.. cut out
..
}
LRESULT KeyboardProc(int code,UINT wparam,ULONG lparam)
{
HFONT hFont;
RECT rect;
UINT my_wparam;
int i,j,k,m;
static BOOLEAN b1=FALSE;
static BOOLEAN CTL_KEY=FALSE;
LOGFONT lf;
MSG my_msg;

ULONG ALT_EXT_KEY_MASK=0x21000000;// bit 29 and 24
// 1098 7654 3210 9876 5432 1098 7654 3210
// 0010 0001 0000 0000 0000 0000 0000 0000
chCharCode = (TCHAR) wParam; // character code
lKeyData = lParam; // key data
if ((lparam & KF_EXTENDED)!=0) return(0L);
if ((lparam & KF_ALTDOWN)!=0) return(0L);
if ((lparam & ALT_EXT_KEY_MASK)!=0) return(0L);
if (wparam==17)
{
if (lparam!=0xc01d0001) CTL_KEY=TRUE;else CTL_KEY=FALSE;
}
if ((lparam & KF_MENUMODE)!=0) return(0L);
if (CTL_KEY) return(0L);
if ((lparam&0x80000000)!=0)
{
if (wparam==VK_SHIFT)
shift_flag=FALSE;
return(1L);
}
if (wparam!=old_wparam) old_wparam=wparam;
if (wparam==VK_SHIFT)
shift_flag=TRUE;
fhWnd=GetFocus();
if (my_hWnd==NULL)
my_hWnd=FindWindow(NULL,"Keyboard");
if (my_hWnd==NULL)
{
MessageBox(NULL, "Fail to Find Keyboard Map", NULL, MB_OK | MB_ICONHAND);
return(0L);
}

if (my_hWnd!=fhWnd)
{
hFont = (HFONT)SendMessage(fhWnd, WM_GETFONT, 0, 0);
GetObject(hFont, sizeof(LOGFONT), &lf);

if (strcmp(font_face,lf.lfFaceName)!=0)
{
strcpy(font_face,lf.lfFaceName);
if (my_hWnd!=GetFocus())
{
my_msg.hwnd=my_hWnd;
my_msg.message=WM_CHAR;
my_msg.wParam='f';
my_msg.lParam=0L;
my_msg.time=GetTickCount();
if (GetWindowRect(my_hWnd,&rect))
{
my_msg.pt.x=rect.left+(rect.right-rect.left)/2;
my_msg.pt.y=rect.top+(rect.bottom-rect.top)/2;
} else
GetCursorPos(& my_msg.pt);
DispatchMessage(&my_msg);
}
}
}

if (wparam==VK_F11)
{
prev_page=page_number;
++page_number;
if (page_number>3) page_number=0;
sprintf(t1_str,"VK_F11 %d",page_number);
MessageBox(NULL, t1_str, NULL, MB_OK | MB_ICONHAND);
if (my_hWnd!=fhWnd)
{
sprintf(t1_str,"%d",page_number);
my_msg.hwnd=my_hWnd;
my_msg.message=WM_CHAR;
my_msg.wParam=t1_str[0];
my_msg.lParam=0L;
my_msg.time=GetTickCount();
if (GetWindowRect(my_hWnd,&rect))
{
my_msg.pt.x=rect.left+(rect.right-rect.left)/2;
my_msg.pt.y=rect.top+(rect.bottom-rect.top)/2;
} else
GetCursorPos(& my_msg.pt);
DispatchMessage(&my_msg);
}
return(0L);
}
k=-1;m=-1;
i=1;
for (j=0;(j<100) && (k==-1);++j)
{

if (char_key[0][i][j]==wparam)
{
k=j;m=i;
}
}
if (k!=-1)
{
i=page_number;
sprintf(t1_str,"Key %d %d=%d %d",++xxx1,wparam,char_key[i][m][k],lparam);
// MessageBox(NULL, t1_str, NULL, MB_OK | MB_ICONHAND);
// wparam=char_key[i][m][k];
my_msg.hwnd=GetFocus();
my_msg.message=WM_CHAR;
if (shift_flag)
m=1;else m=0;
my_msg.wParam=char_key[i][m][k];
my_msg.lParam=0L;
my_msg.time=GetTickCount();
GetCursorPos(& my_msg.pt);
if ((page_number>0) &&
((wparam>='A') && (wparam<='Z')))
{
DispatchMessage(&my_msg);
return(1L);
}
}
return(0L);
}


BOOL APIENTRY DllMain(HANDLE hInst, DWORD ul_reason_being_called, LPVOID lpReserved)
{
int i;
switch (ul_reason_being_called)
{
key_hook=SetWindowsHookEx(WH_KEYBOARD,(long) KeyboardProc,hInst,0);
if (key_hook==NULL)
MessageBox(NULL, "Can not hook the keyboard.", NULL, MB_OK | MB_ICONHAND);
break;
case DLL_PROCESS_DETACH:
UnhookWindowsHookEx(key_hook);
break;
}
}

Posted by r_z_aret@pen_fact.com on September 23rd, 2004


I don't think your response answered any of my questions. I'll try
again.

What is the "keymap"? Is it a dialog box? Is it generated by code you
wrote? ???

What code opens the "keymap" or the window that holds it? Is that code
in a.dll or b.exe or somewhere else?

If you have any control over the code that opens the "keymap" or the
window that contains it, then you can modify that code to keep a copy
of the window handle for the keymap. And then you can use that handle
in calls to SendMessage, etc. Using GetFocus is only appropriate if
you have no more direct method.

On Tue, 21 Sep 2004 17:18:03 GMT, Tuy Solang <solang@verizon.net>
wrote:

-----------------------------------------
To reply to me, remove the underscores (_) from my email address (and please indicate which newsgroup and message).

Robert E. Zaret, eMVP
PenFact, Inc.
500 Harrison Ave., Suite 3R
Boston, MA 02118
www.penfact.com

Posted by Tuy Solang on September 26th, 2004


Hi,

I made a mistake by sending DispatchMessage to the un-focus window,
instead of using SendMessage. Now, the DLL and the keymap programs
are working properly.

Thanks for your help.


Similar Posts