Remove full path of PDB file from C# dll or exe. (/pdbaltpath in c#)

I came across a challenge last week, wherein I was asked to remove the path information from the dll files which were build inside my project.

For those who don’t have an idea about what I am talking about, here is a quick introduction. The dll/exe files which are build by Visual Studio contains the Win32 PE header information, and this is true for all the dll and exe on Win32 platforms. Read about it at (http://en.wikipedia.org/wiki/Portable_Executable)

To check the headers in your dll/exe files you can use the dumpbin tool.

dempbin


So you can see the full path of the PDB file is embedded inside the DLL file, which is the point of concern as you might not want to expose private information in your releases. Fortunately this can be avoided by using the linker argument “/PDBALTPATH:%_PDB%” which just embeds the filename of PDB instead of full path. Unfortunately, this is true only for VC/C++ and there is no way to use this method when you are building C# assemblies.

So I started reading the internet to find a quick hack for it. Some suggested rebase.exe and some suggested “binplace.exe”. Binplace comes with Microsoft DDK. Nothing worked for me, and hence you are reading this post Smile

I decided to strip out the file path information myself, and so started reading on PE header format. I saw some interesting article already written on the same problem.

http://www.debuginfo.com/examples/debugdir.html

I used the code provided in above article and tweaked it to make it more user friendly. My code is variant of DebugDir.cpp written by  Oleg Starodumov.

I renamed the build to have a output called “DebugInfo.exe” and which has 2 arguments.

Usage of DebugInfo.exe:

1. Just to list the debug directories

c:\DebugInfo {filename.(exe|pdb)}

debuginfo-0

2. To remove complete PDB file information

c:\DebugInfo {filename.(exe|pdb)} clean

debuginfo-1

3. To remove just the path information of PDB and retain the filename

c:\DebugInfo {filename.(exe|pdb)} clean-path

debuginfo-2

You can verify the headers again using dumpbin.exe. Find the source code from the SourceForge site listed below. It is compiled and build using Visual Studio 2010 (VC++ Win32 Console application), add imagehlp.lib to your linker.

 

SourceForge: https://sourceforge.net/projects/debugdir/

Have fun!!!

-Vaibhav Gaikwad

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

User32's SetForegroundWindow() API in C#

If there occurs a need to check if some process is already running and then bringing that process' main window in front then .NET does not helps 100%.

Using the System.Diagnositics.Process class to find out a already running process is a easy task but to bring that process' main window in front is a difficult goal.

Here's the code to get the already running process (e.g. Notepad) and then bringing it to front, using the native API from User32.dll >> SetForegroundWindow(int WindowHandle)

[System.Runtime.InteropServices.

class Program
{
static void Main(string[] args)
{
 System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("notepad");
   if (p.Length > 0)
   {
     SetForegroundWindow(p[0].MainWindowHandle);
   }
 }
}

DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

-Vaibhav Gaikwad

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Flashing your window, getting attention.

This is a simple example of using Win32 api in .NET. I'll use the FlashWindow() API from user32.dll so as to make the window go flashing and get attention.

Take a simple Windows form and add a button to it. Add a reference to System.Runtime.InteropServices namespace. And heres the code.

// --------- code -----------
[DllImport("user32")]
public static extern int FlashWindow(int hWnd, int bInvert);

public partial class Form1 : Form
{
   public Form1()
   {
      InitializeComponent();
   }

   private void button1_Click(object sender, EventArgs e)
   {
      FlashWindow(this.Handle.ToInt32(), 1);
   }
} // -------- code ends

This was nice and simple for the start.

-Vaibhav Gaikwad

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Where to start from ?

Thinking of where to start from with millions of topics in mind. I will dedicate this blog to unmanaged code. As a .NET programmer, I will be illustrating use of Win32 native code in managed .NET code.

Right form the start, I was really interested in exploring the Interop framework of .NET.

So this blog will be a mix of both, unmanged in managed world.

-Vaibhav Gaikwad

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910