Call aaapi_initialize return BadImageFormatException in c# using wrapper

Using C# (in Visual Studio 2017), I am writing a class with a function LoginServer() that calls the ProjectWise API to login, and ready to make further action upon users' request.

This dll will be deployed to MicroStation as a toolbar plugin. 

I'm trying to call aaApi_Initialize() from C# or VBA, but haven't succeeded with either one.

The C# code is as follows:

protected void LoginServer()
{
    LoginTime = DateTime.Now;
    NormalLogin = new ProjectWiseLogin();
    AdminLogin = new ProjectWiseLogin();
    PWWrapper.aaApi_Initialize(1);

    Debug.WriteLine("Initialize ProjectWise normal login");
    if (PWWrapper.aaApi_Login(DataSourceType.Unknown, Settings.PWServer, Settings.PWUserID, Settings.PWPassword, null))
    {
        NormalLogin.DataSourceId = PWWrapper.aaApi_GetActiveDatasource();
        NormalLogin.UserId = PWWrapper.aaApi_GetCurrentUserId();
        Logger.Debug("Login ProjectWise with normal account succeeded (session {0})", NormalLogin.DataSourceId);
    }
    else
    {
        Logger.Error(String.Format("Login ProjectWise using account \"{0}\" failed", Settings.PWUserID));
    }
}

And a wrapper class is written in c#, with dllimport from dmscli.dll.

public static class PWWrapper
{
    static PWWrapper()
    {
        // Add ProjectWise DLL to system search path
        var combinedPath = String.Format("{0}{1}{2}", Environment.GetEnvironmentVariable("PATH"),
            System.IO.Path.PathSeparator, ConfigurationManager.AppSettings["ProjectWiseDllPath"]);
        Environment.SetEnvironmentVariable("PATH", combinedPath);
    }
    [DllImport("dmscli.dll", CharSet = CharSet.Unicode)]
    internal static extern bool aaApi_Initialize(int init);

    [DllImport("dmscli.dll", CharSet = CharSet.Unicode)]
    internal static extern bool aaApi_Uninitialize();
}

I am using dmscli.dll from C:\Program Files\Bentley\ProjectWise\bin so I assume that all dll used is in 64 bit.,so my c# build is targeting Any CPU / x64.

However when running this dll either alone or from MicroStation, BadImageFormatException is always thrown in the line aaApi_Initialize(),, which is usually shown when 32 and 64 bit dll are running together.

This only happens when the API calls occurs; if I rebuild the dll with the API calls commented out, then the C# code runs fine.

Same thing happens when I am uising VBA in Microstation and try to call dmscli.dll by refercing by code.

Public Declare PtrSafe Function aaApi_Initialize Lib "dmscli.dll" (ByVal ulModule As Long) As Integer
Public Declare PtrSafe Function aaApi_Login Lib "dmscli.dll" (lDSType As Long, ByVal lptstrDataSource As String, ByVal lpctstrUsername As String, ByVal lpctstrPassword As String, ByVal lpctstrSchema As String) As Long

Did anyone tried to call aaApi_Initialize in c#/vba?

Any help will be appreciated.

  • The exception that is thrown when the file image of a dynamic link library (DLL) or an executable program is invalid. If you get a BadImageFormatException when interfacing with a native DLL, it almost always means that you are trying to interface with a 32-bit DLL while running in the 64-bit CLR, or vice versa. In most cases you might be facing the problem with your website after deploying on server.

    Make sure that you are not having 32-bit / 64-bit conflict. So, you need to adjust your application pool to Enable 32-Bit or 64-Bit accordingly. Set the Target platform setting on your c# EXE project, not your class library project. Alternatively, you can ship both 32-bit and 64-bit DLLs with different file names, define separate P/Invoke stubs for each version, and decide which one to call at runtime. The easiest way to do this would probably be to wrap your native calls in an interface (e.g., INativeMethods) and choose which implementation to instantiate at runtime based on IntPtr.Size. With this method, you could still target AnyCPU.

  • You might want to take a look at 's wrapper code that he has made available for ideas on how to handle both 32 and 64 bit code.  You can find his "MostOfDavesClasses" here:  https://github.com/DaveBrumbaugh/MostOfDavesClasses-CSharp-Wrappers-For-ProjectWise