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.

Parents Reply Children
No Data