Monday, November 30, 2009

How to use an unmanaged DLL from another location

//[DllImport("someDll.dll")]
//private static extern uint someMethod(string s);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate uint someMethod(string s);


[DllImport("kernel32.dll", SetLastError=true)]
private static extern IntPtr LoadLibrary(string dllToLoad);

[DllImport("kernel32.dll")]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

[DllImport("kernel32.dll")]
private static extern bool FreeLibrary(IntPtr hModule);


private static IntPtr libSomeDll;
private static IntPtr addrSomeMethod;


libSomeDll = LoadLibrary(Path.Combine(path, "someDll.dll"));
if (libSomeDll == IntPtr.Zero)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "LoadLibrary failed for someDll.dll.");
}

addrSomeMethod = GetProcAddress(libSomeDll, "someMethod");
if (addrSomeMethod == IntPtr.Zero)
{
throw new ApplicationException("GetProcAddress failed for someMethod.");
}


someMethod methSomeMethod = (someMethod)Marshal.GetDelegateForFunctionPointer(addrSomeMethod, typeof(someMethod));
uint result = methSomeMethod("");

bool result2 = FreeLibrary(libSomeDll);

Monday, November 09, 2009

How to use the OR operator in grep instead of the Windows pipe symbol

Doesn't work:
egrep firstTerm|secondTerm file.txt

egrep: (standard input): Not enough space
'SW1' is not recognized as an internal or external command,
operable program or batch file.

Works: (caret '^' escape symbol)
egrep firstTerm^|secondTerm file.txt

Friday, November 06, 2009

Visual Studio Edit and Continue

EDIT: LOOKS LIKE THIS IS ANOTHER ONE THAT DIDN'T ACTUALLY FIX IT... :-(
PLEASE LET ME KNOW IF YOU HAVE THE SOLUTION


A certain Visual Studio project wouldn't let me edit the code while debugging. It made debugging a real pain. I would get messages like this:

"Changes are not allowed when the debugger has been attached to an already running process or the code being debugged was optimized at build or run time."

There are different reasons why this type of message might come up. But in my case, eventually I discovered that the projects target frameworks didn't match. One project targeted 3.5 and the other targeted 2.0. Once I set both to 3.5, I could debug normally again.