Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6192311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:55:18+00:00 2026-05-24T02:55:18+00:00

I am using a third party dll built with VS2008 that relies on msvcr90.dll

  • 0

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]));
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:55:19+00:00Added an answer on May 24, 2026 at 2:55 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a third party dll that is exposed via a COM Interop
A website I'm working on is using a third-party assembly, let's say A.dll. This
There is a bug in a third party .DLL file that we're using and
I am using a third-party dll that requires an IPEndPoint. As the user can
I am using a third party .Net dll in my code and when I
I'm using a third party static library in my C++ project that has a
I'm using a third-party AJAX slideshow for a website that takes an RSS feed
I am using a third-party Windows Forms control that performs some actions delayed using
I have a VC++ application compiled against a third party DLL (using their LIB
I'm using third party libraries that I obtained well before KB2465367 came out. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.