Inserting a tool bar button to the Windows explorer from shellextension.

S

Shafiq

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;
}
 
B

Ben Voigt [C++ MVP]

Shafiq said:
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.

Pretty sure writing shell extensions in .NET is a no-no. (Mainly because
each process can have only one CLR version loaded)
 
C

Chris Mullins [MVP - C#]

[Shell Extension]

Not that this answers your question, but there's a big problem writing shell
extensions in .Net. The shell is a single process, and any process can only
have a single version of .Net running.

This means the shell may already be hosting a .Net 1.1. app, and your .Net
2.0 app therefore can't load. This will be very frustrating for both you and
your users.

Now with that said, what you're look for are called Band Objects, and there
are a number of toolkits out there for this. A good place to start would be:
http://www.codeproject.com/KB/cs/BandObjects20.aspx?print=true
 
B

Ben Voigt [C++ MVP]

Chris Mullins said:
[Shell Extension]

Not that this answers your question, but there's a big problem writing
shell extensions in .Net. The shell is a single process, and any process
can only have a single version of .Net running.

This means the shell may already be hosting a .Net 1.1. app, and your .Net
2.0 app therefore can't load. This will be very frustrating for both you
and your users.

Bigger problem -- shell extensions are loaded into any program using the
common dialog boxes (at least File Open and File Save), so you have to worry
about CLR conflicts not just in the shell but with practically every single
other program on the computer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top