Reputation: 916
I am using a third party dll built with VS2008 that relies on msvcr90.dll
for printing to stdout
. I try to redirect this output with SetStdHandle
, but the dll seems to miss a final call to flush the output buffer before returning.
Simply using
[DllImport("msvcr90.dll")]
leads to a DllNotFoundException.
To solve the problem, I PInvoke fflush()
and __iob_func()
from msvcr90.dll
which resides in my WinSxS folder. I did this by setting the full path to the dll (I got the path using DependencyWalker on the third party dll). What is of course not very usefull if I want to give my program to other people or if msvcr90.dll
is updated by Microsoft.
Can anybody give me a hint, how to tell DllImport to use the recent version of msvcr90.dll
?
Thanks for any reply!
Andreas
My Solution
With Davids help I came up with the following solution. msvcr90.dll
is not located through WinSxS, but taken from the loaded module list.
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
namespace siemens.OptiX
{
static unsafe class FlushPrintBuffer
{
struct CFile
{
char* _ptr;
int _cnt;
char* _base;
int _flag;
public int _file;
int _charbuf;
int _bufsiz;
char* _tmpfname;
}
[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate CFile* iobFuncDelegate();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate int flushDelegate(CFile* stream);
static iobFuncDelegate myIobFunc;
static flushDelegate myFlush;
static FlushPrintBuffer()
{
ProcessModuleCollection allLoadedModules =
Process.GetCurrentProcess().Modules;
IntPtr aDllHandle = allLoadedModules
.Cast<ProcessModule>()
.Where(p => string.Compare(p.ModuleName, "msvcr90.dll", true) == 0)
.First()
.BaseAddress;
IntPtr aIobFuncPtr = GetProcAddress(aDllHandle, "__iob_func");
myIobFunc = Marshal.GetDelegateForFunctionPointer(
aIobFuncPtr, typeof(iobFuncDelegate)) as iobFuncDelegate;
IntPtr aFlushPtr = GetProcAddress(aDllHandle, "fflush");
myFlush = Marshal.GetDelegateForFunctionPointer(
aFlushPtr, typeof(flushDelegate)) as flushDelegate;
}
static public void flushNow()
{
CFile* aFilePtr = myIobFunc();
int aRes = myFlush(&(aFilePtr[1]));
}
}
}
Upvotes: 2
Views: 1196
Reputation: 612964
I think you will find it hard to use DLLImport
to link to the right runtime. This is because your executable will be missing the necessary manifest to make the WinSxS magic work.
I would declare some delegates for the functions you need to import. Use GetProcAddress
to obtain the function pointers. You convert the function pointers into delegates with Marshal.GetDelegateForFunctionPointer
.
In order to get the module handle to pass to GetProcAddress
you can use GetModuleHandle(@"msvcr90.dll")
since you know that this is loaded into your process.
Get hold of GetProcAddress
and GetModuleHandle
with P/invoke.
It seems calling GetModuleHandle(@"msvcr90.dll")
will not work. Calling GetModuleHandle
with the full path would work, but that's not really a tenable solution.
Instead it is easy enough to enumerate the loaded modules like this:
IntPtr MyGetModuleHandleByPartialName(string ModuleName)
{
foreach (ProcessModule module in Process.GetCurrentProcess().Modules)
if (module.FileName.ToLower().Contains(ModuleName))
return module.BaseAddress;
return IntPtr.Zero;
}
Upvotes: 2