Jump to content


Structure Calculation


  • Please log in to reply
5 replies to this topic

#1 Guests

  • Guests

Posted 21 November 2011 - 08:25 AM

Hi,

How do I calculate the size of each element in a structure? For example, there is a structure called _NOTIFYICONDATA and I'm trying to know the sizes for the 64 bit build. MSDN says, ( <!-- m -->http://msdn.microsof... ... 73352.aspx<!-- m --> )
typedef struct _NOTIFYICONDATA {
  DWORD cbSize;
  HWND  hWnd;
  UINT  uID;
  UINT  uFlags;
  UINT  uCallbackMessage;
  HICON hIcon;
  TCHAR szTip[64];
  DWORD dwState;
  DWORD dwStateMask;
  TCHAR szInfo[256];
  union {
    UINT uTimeout;
    UINT uVersion;
  };
  TCHAR szInfoTitle[64];
  DWORD dwInfoFlags;
  GUID  guidItem;
  HICON hBalloonIcon;
} NOTIFYICONDATA, *PNOTIFYICONDATA;
Assuming there is hIcon already and the script is trying to set it as the tray icon. How do I set the offsets of each NumPut command? I'd like to know how the offets are calculated such as 4, 8, 12, 20. And what numbers are supposed to be put for the 64 bit build?

By the way I put "uint" for the type parameter since most x64 compatible scripts do it.
#NoTrayIcon      
#Persistent
Type := (A_PtrSize=8) ? "uint" : ""
VarSetCapacity( NID, 444, 0 )
NumPut( 444, NID, Type )
NumPut( A_ScriptHwnd, NID, 4 + ((A_PtrSize=8) ? 4 : 0), Type)
NumPut( 1028, NID, 8 + ((A_PtrSize=8) ? 4 : 0), Type)
NumPut( 2, NID, 12 + ((A_PtrSize=8) ? 4 : 0), Type )
NumPut( hIcon, NID, 20 + ((A_PtrSize=8) ? 4 : 0), Type)

Menu, Tray, Icon                                           ; Show the default Tray icon ..
DllCall( "shell32\Shell_NotifyIcon", Ptr,0x1, Ptr, &NID ) ; .. and immediately modify it
Thanks.

#2 Odlanir

Odlanir
  • Members
  • 754 posts

Posted 21 November 2011 - 08:34 AM

Structures

#3 Guests

  • Guests

Posted 07 December 2011 - 06:22 PM

Is this calculation correct?
/*
typedef struct _NOTIFYICONDATA {
http://msdn.microsoft.com/en-us/library/windows/desktop/bb773352%28v=vs.85%29.aspx
x86Ansi	x86Uni	x64Uni	x64Alignment
0  		0  		0  	 	0  	 	DWORD cbSize;
4  		4  		4  	 	8  	 	HWND  hWnd;
8  		8  		12 	 	16 	 	UINT  uID;
12 		12 		16 	 	20 	 	UINT  uFlags;
16 		16 		20 	 	24 	 	UINT  uCallbackMessage;
20 		20 		24 	 	28 	 	HICON hIcon;
24 		24 		32 	 	36 	 	TCHAR szTip[64]; 128 bytes for Unicode
88 		152		160		164		DWORD dwState;
92 		156		164		168		DWORD dwStateMask;
96 		160		168		172		TCHAR szInfo[256];	512 for Unicode TCHAR is 1 byte for ANSI 2 bytes for Unicode
352		672		680		684		union {
--- 	--- 	--- 	--- 	UINT uTimeout;
--- 	--- 	--- 	--- 	UINT uVersion;
--- 	--- 	--- 	--- 	};
356		676		684		688		TCHAR szInfoTitle[64]; 128 bytes for Unicode
420		804		812		816		DWORD dwInfoFlags;
--- 	--- 	--- 	--- 	GUID  guidItem;
--- 	--- 	--- 	--- 	typedef struct _GUID {
424		808		816		820		DWORD Data1;
428		812		820		824		WORD  Data2;	2 bytes
430		814		822		826		WORD  Data3;
432		816		824		828		BYTE  Data4[8]; 1 byte * 8
--- 	--- 	--- 	--- 	} GUID;
440		824		832		836		HICON hBalloonIcon;
444		828		840		844
} NOTIFYICONDATA, *PNOTIFYICONDATA;
*/
It seems it has an error in the x64Alignment column. just me's struct library returns 848 in size on x64.
#Include <Class_DllStruct>		;[CLASS] DllStruct (AHK 1.1) by just me http://www.autohotkey.com/forum/topic74240.html
structGUID := "
(LTrim Join
   DWORD Data;
   WORD  Data2;
   WORD  Data3;
   BYTE  Data4[8];
)"
   
;http://msdn.microsoft.com/en-us/library/windows/desktop/bb773352%28v=vs.85%29.aspx
structNOTIFYICONDATA := "
(LTrim Join
   DWORD cbSize;
   HWND  hWnd;
   UINT  uID;
   UINT  uFlags;
   UINT  uCallbackMessage;
   HICON hIcon;
   TCHAR szTip[64];
   DWORD dwState;
   DWORD dwStateMask;
   TCHAR szInfo[256];
   UINT  uTimeout;
   TCHAR szInfoTitle[64];
   DWORD dwInfoFlags;
   " . structGUID . "
   HICON hBalloonIcon;
)"

; msgbox % def_NOTIFYICONDATA
_NOTIFYICONDATA := new DllStruct(structNOTIFYICONDATA)
msgbox % "Address:" _NOTIFYICONDATA.GetPtr() "`nSize: " _NOTIFYICONDATA.GetSize()


#4 42

42
  • Guests

Posted 08 December 2011 - 02:53 AM

hIcon is aligned incorrectly: 28 is not a multiple of 8.

#5 Guests

  • Guests

Posted 08 December 2011 - 03:05 AM

Does that mean these are all incorrect? They aren't multiple of 8 either.
20 uFlags
28 hIcon
36 szTip[64]
164 dwState
172 szInfo[256]
684 uTimeout/uversion
820 Data1
826 Data3
828 Data4[8]
836 hBalloonIcon


#6 chaidy

chaidy
  • Members
  • 19 posts

Posted 04 May 2012 - 08:56 AM

i did test. 64bit hIcon offset = 32
[attachment=31:NotifyIconData Struct.jpg]
Test Code:
; CStruct.ahk - http://www.autohotkey.com/community/viewtopic.php?f=2&t=84054
#include CStruct.ahk
#SingleInstance Force

^\::ExitApp

F12::
	;tray icon change to notepad.exe main icon.
	FileName := "c:\windows\notepad.exe"
	NID := new CNotifyIconData
	NID.hwnd := DllCall( "FindWindow", Str,"AutoHotkey" , Str, A_ScriptFullPath ( A_IsCompiled ? "" : " - AutoHotkey v" A_AhkVersion ) )
	NID.uID := 1028
	NID.uFlags := 0x2|0x4
	NID.szTip := "Test tooltip..."
	NID.hIcon := DllCall("Shell32\ExtractAssociatedIcon" (A_IsUnicode ? "W" : "A")
		  , ptr, DllCall("GetModuleHandle", ptr, 0, ptr), str, FileName, "ushort*", lpiIcon, ptr) 
	NID.TreeView() ; <<== checking struct data.
	DllCall( "Shell32\Shell_NotifyIcon" (A_IsUnicode ? "W" : "A") , UInt,0x1, UInt,NID[""] )
	DllCall( "DestroyIcon", UInt,NID.hIcon )
return

struct class making model:

/*  this class defined in CStruct.ahk

;http://msdn.microsoft.com/en-us/library/windows/desktop/bb773352.aspx
;----------------------------------------------------------------------
class CNotifyIconData extends CStruct_Base
{
	__New()
	{
		this.AddStructVar("cbSize"	, "DWORD")
		this.AddStructVar("hWnd"	, "HWND")
		this.AddStructVar("uID"		, "UINT")
		this.AddStructVar("uFlags"	, "UINT")
		this.AddStructVar("uCallbackMessage", "UINT")
		this.AddStructVar("hIcon"		, "HICON")
		this.AddStructVar("szTip"		, "TCHAR" , 64)
		this.AddStructVar("dwState"		, "DWORD")
		this.AddStructVar("dwStateMask"	, "DWORD")
		this.AddStructVar("szInfo"		, "TCHAR" , 256)
		this.AddStructVar("uTimeout"	, "UINT"  , "union_start")
		this.AddStructVar("uVersion"	, "UINT"  , "union_end")
		this.AddStructVar("szInfoTitle"	, "TCHAR" , 64)
		this.AddStructVar("dwInfoFlags"	, "DWORD")
		this.AddStructVar("guidItem"	, "CGuid")	; use CGuid class
		this.AddStructVar("hBalloonIcon", "HICON")
		this.SetStructCapacity()
		
		this.cbSize := this.size
	}
}
*/