Re: load icon in dll file not work ?! by oneneno
oneneno
Mon Feb 25 04:34:00 CST 2008
Ok here are my code
//************ trayicon.Dll module***************//
#define TRAY_NOTIFYICON WM_USER + 2001
#define ID_TRAY 5000
#define ID_NOTIFY 6000
static const GUID guidPPCAlertsApp = { 0xca777af7, 0x3cde, 0x4c15, { 0xa7,
0xcd, 0x81, 0xf6, 0x29, 0xab, 0xa6, 0xb6 } };
//static const GUID guidPPCAlertsApp = { 0x33765136, 0x8cb9, 0x449a, { 0xb0,
0x20, 0x43, 0xed, 0x40, 0xa, 0xb8, 0xfc } };
// Global Variables:
HINSTANCE g_hInst; // current instance
HWND g_hWndMenuBar; // menu bar handle
TCHAR szTitle[MAX_LOADSTRING]; // title bar text
HICON hTitleBarIcon;
BOOL APIENTRY DllMain (HANDLE hModule, DWORD reason, LPVOID reserved)
{
return TRUE;
}
DWORD WINAPI main_thread (LPVOID param)
{
WinMain(NULL, NULL, _T(""), 0);
return 0;
}
extern "C" __declspec(dllexport) int startmod (void *param) {
if (CreateThread(NULL, 0, main_thread, param, 0, NULL) == NULL) {
return -1;
}
return 0;
}
// Tray Icons and Notification
BOOL TrayMessage(
HWND hwnd,
DWORD dwMessage,
UINT uID,
HICON hIcon,
PTSTR pszTip
)
{
BOOL res = FALSE;
NOTIFYICONDATA tnd;
tnd.cbSize = sizeof(NOTIFYICONDATA);
tnd.hWnd = hwnd;
tnd.uID = uID;
tnd.uFlags = NIF_MESSAGE|NIF_ICON;
tnd.uCallbackMessage = TRAY_NOTIFYICON;
tnd.hIcon = hIcon;
tnd.szTip[0] = '\0';
res = Shell_NotifyIcon(dwMessage, &tnd);
return res;
}
void TrayIconDelete(
HWND hwnd,
UINT uID,
HICON hIcon,
PTSTR pszTip
)
{
TrayMessage(hwnd, NIM_DELETE, uID, hIcon, NULL);
}
void TrayIconModify(
HWND hwnd,
UINT uID,
HICON hIcon,
PTSTR pszTip
)
{
//animate icons
TrayMessage(hwnd, NIM_MODIFY, uID, hIcon, NULL);
}
void TrayIconAdd(
HWND hwnd,
UINT uID,
HICON hIcon,
PTSTR pszTip
)
{
TrayMessage(hwnd, NIM_ADD, uID, hIcon, NULL);
}
void AddNotificationIcon(HWND hWnd,UINT uID,TCHAR *wndTittle, HICON hIcon)
{
SHNOTIFICATIONDATA Notification;
Notification.cbStruct = sizeof(SHNOTIFICATIONDATA);
Notification.dwID = uID;
Notification.npPriority = SHNP_INFORM;
Notification.csDuration = 1; // in second
Notification.hicon = hIcon;
Notification.clsid = guidPPCAlertsApp;
Notification.grfFlags = 0;
Notification.pszTitle = (LPCTSTR) wndTittle;
Notification.pszHTML = _T("<html><b>module is ready</b></html>"); // i can
see this popup but no icon ?!
Notification.hwndSink = hWnd;
Notification.rgskn[0].pszTitle = (LPCTSTR) wndTittle;
Notification.rgskn[0].skc.wpCmd = 1003;
Notification.rgskn[0].skc.grfFlags = NOTIF_SOFTKEY_FLAGS_HIDE;
SHNotificationAdd(&Notification);
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
#ifndef WIN32_PLATFORM_WFSP
HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TRAYICON));
#endif // !WIN32_PLATFORM_WFSP
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
#ifndef WIN32_PLATFORM_WFSP
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
#endif // !WIN32_PLATFORM_WFSP
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
//TCHAR szTitle[MAX_LOADSTRING]; // title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // main window class name
g_hInst = hInstance; //hInstance; // Store instance handle in our global
variable
#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
// SHInitExtraControls should be called once during your application's
initialization to initialize any
// of the device specific controls such as CAPEDIT and SIPPREF.
SHInitExtraControls();
#endif // WIN32_PLATFORM_PSPC || WIN32_PLATFORM_WFSP
LoadString(g_hInst, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(g_hInst, IDC_TRAYICON, szWindowClass, MAX_LOADSTRING);
#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
//If it is already running, then focus on the window, and exit
hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
// set focus to foremost child window
// The "| 0x00000001" is used to bring any owned windows to the
foreground and
// activate them.
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
showdebug(L"Application alredy loaded");
return 0;
}
hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
//set focus to foremost child window
//The "| 0x00000001" is used to bring any owned windows to the
foreground and
//activate them.
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
showdebug(L"Application alredy loaded");
return 0;
}
#endif // WIN32_PLATFORM_PSPC || WIN32_PLATFORM_WFSP
if (!MyRegisterClass(g_hInst, szWindowClass))
{
showdebug(L"could not register class");
return FALSE;
}
hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL,
NULL, hInstance, NULL);
// hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
// 0, 0, 0, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
showdebug(L"could not create windows");
return FALSE;
}
#ifdef WIN32_PLATFORM_PSPC
// When the main window is created using CW_USEDEFAULT the height of the
menubar (if one
// is created is not taken into account). So we resize the window after
creating it
// if a menubar is present
if (g_hWndMenuBar)
{
RECT rc;
RECT rcMenuBar;
GetWindowRect(hWnd, &rc);
GetWindowRect(g_hWndMenuBar, &rcMenuBar);
rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);
MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left,
rc.bottom-rc.top, FALSE);
}
#endif // WIN32_PLATFORM_PSPC
hTitleBarIcon = LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_ICON1));
AddNotificationIcon(hParent,ID_NOTIFY,szTitle,hTitleBarIcon);
showdebug(L"AddNotificationIcon done"); // but no icon appear !
HICON hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON2));
TrayIconAdd(hParent, ID_TRAY,hIcon, NULL); // but no icon appear !
//ShowWindow(hWnd, SW_HIDE);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//************in Main proccess exe file***************//
if( (hmodule= LoadLibrary(_T("trayicon.dll"))) == NULL)
{
showdebug(L"module not started.");
}
else
{
startmod = (module_icon)GetProcAddressA(hmodule, "startmod");
}
if (startmod) {
showdebug(L"module started."); // ok
startmod(NULL, 0);
}
I have tried to pass HWND and hInstance from main proccess to trayicon.dll,
but no success. what have i doing wrong ?
"Scott Seligman" wrote:
> =?Utf-8?B?a25vcHBlcg==?= <oneneno@gmx.net> wrote:
> >
> >we have to write a module with icon and system tray on pocket pc (WM6). Whan
> >i start the module as exe file. All icon can loaded, but when i compile the
> >module as dll file and loaded in main exe app, the icons will not appear.
> >I have tried to pass the hInstance from main process to loadIcon function
> >but no result. My question it is possible to load icon in dll file generally ?
>
> Yes, it's possible to load an icon from a DLL.
>
> If you'd like some more help, post code that's failing.
>
> --
> --------- Scott Seligman <scott at <firstname> and michelle dot net> ---------
> Anger is the most useless emotion, .. destructive to the mind and
> hurtful of the heart.
> -- Henchick in Dark Tower, Song of Susannah by Stephen King
>