|
Hi,
I am trying to insert a new toolbar button to the windows explorer menu. I an able to locate the correct ToolbarWindow32, and inserted a button using the code snippet shown below. However the toolbar button text is coming in jung characters. Any help will be greatly appreciated.
Int32 iBitmap = 0;
TBBUTTON Button = new TBBUTTON();
Button.idCommand = 1000;
Button.fsState = (byte)TBSTATE.TBSTATE_ENABLED;
Button.fsStyle = (byte)(BTNS.BTNS_BUTTON | BTNS.BTNS_AUTOSIZE | BTNS.BTNS_SHOWTEXT);
Button.dwData = 0;
Button.iString = Marshal.StringToHGlobalUni("test Menu\0");
Button.iBitmap = iBitmap;
Result = SetToolBarButton(toolbarhwnd, buttoncount, ref Button);
private unsafe bool SetToolBarButton(IntPtr hToolbar, int index, ref TBBUTTON tbButton)
{
// One page
const int BUFFER_SIZE = 0x1000;
byte[] localBuffer = new byte[BUFFER_SIZE];
UInt32 processId = 0;
UInt32 threadId = Win32.GetWindowThreadProcessId(hToolbar, out processId);
IntPtr hProcess = Win32.OpenProcess(ProcessRights.PROCESS_ALL_ACCESS, false, processId);
if (hProcess == IntPtr.Zero)
{
Debug.Assert(false);
return false;
}
IntPtr ipRemoteBuffer = Win32.VirtualAllocEx(hProcess, IntPtr.Zero, new UIntPtr(BUFFER_SIZE), MemAllocationType.COMMIT, MemoryProtection.PAGE_READWRITE);
if (ipRemoteBuffer == IntPtr.Zero)
{
Debug.Assert(false);
return false;
}
fixed (TBBUTTON* pTBButton = &tbButton)
{
IntPtr ipTBButton = new IntPtr(pTBButton);
// this is fixed
Int32 dwBytesWritten = 0;
IntPtr ipBytesWritten = new IntPtr(&dwBytesWritten);
bool b4 = Win32.WriteProcessMemory(hProcess,
ipRemoteBuffer,
ipTBButton,
new UIntPtr((uint)sizeof(TBBUTTON)),
out ipBytesWritten);
if (!b4)
{
Debug.Assert(false);
return false;
}
int chars = (int)Win32.SendMessage(hToolbar, (uint)TB.TB_INSERTBUTTON, (IntPtr)index, ipRemoteBuffer);
if (chars == -1)
{
Debug.Assert(false);
return false;
}
}
Win32.VirtualFreeEx(hProcess, ipRemoteBuffer, UIntPtr.Zero, MemAllocationType.RELEASE);
Win32.CloseHandle(hProcess);
return true;
}
|
|
|
|