Can't call DLL function that makes ProjectWise API calls

Using Visual C++ (in Visual Studio 2012), I've made a DLL with a function openDoc(int projID,int docID) that calls the ProjectWise API to open a document.

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

The C++ code is as follows:

#include "stdafx.h"
#include "AAAPI.H"
#include "AAWINAPI.H"
#include "AADMSAPI.H"
#include "AAWINDMS.H"

extern "C"

openDoc(int projectID,int docID)

LPCWSTR dbName=L"abc.def:ghi";
LPCWSTR user=L"";
LPCWSTR pwd=L"";
LPCWSTR schema=L"";

bool resultInit=aaApi_Initialize(AAMODULE_ALL);

bool resultLogin=aaApi_Login(AAAPIDB_UNKNOWN,dbName,user,pwd,schema);
bool resultOpen=aaApi_OpenDocument(projectID,docID,false);
return resultOpen;
This builds successfully.  I have tested the body of openDoc() in a C++ Win32 console application and it works.
I tried with C# as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace ProjectWise_Csharp
class Program

public const string dllPath = @"U:\Software Development\c++ projects\ProjectWise2\Debug\ProjectWise2.dll";
[DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)]
public extern static int openDoc(int projID, int docID);

static void Main(string[] args)
Console.WriteLine(openDoc(1799, 29));

This throws a System.BadImageFormatException, with the message:
An attempt was made to load a program with an incorrect format.

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

Everything I've read online indicates that this exception gets thrown when a 64-bit program tries to call a 32-bit DLL, but my DLL is Win32 and I've set my C# project's platform target to x86.

I tried it with VBA as follows:

Public Declare Function openDoc Lib "U:\Software Development\c++ projects\ProjectWise2\Debug\ProjectWise2.dll" (ByVal projectID As Long, ByVal docID As Long) As Variant
Sub TestDLL()
 Debug.Print openDoc(1799, 29)
Exit Sub
This gave the following set of errors:
Last Error [50126]
Cannot execute document action
Specified document does not exist.
Run-time error '49':
Bad DLL calling convention
Since I can run the code fine from C++, I know that's not the problem.  Is there a problem with how I've set up my DLL export?  
Parents Reply Children
No Data