Reputation: 799
I have a 32 bit dll that I need to be called from a 64 bit .NET Core Application.
My method to do this is to create a 32bit console App that basically looks like this:
[DllImport("x.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
static extern int x(int var);
static void Main(string[] args)
{
Console.log("I Started!");
int y = x(0);
//do something with y.
}
And then to call that from my 64 bit Core Application using Process.Start("consoleapp.exe")
When I run the consoleapp.exe file, it prints "I Started!" as expected, and the operation of x.dll also executes correctly.
However, when I call the consoleapp.exe from process.Start() I get an SEHException thrown after "I Started!" is printed to the output (i.e. when the DllImport part is hit).
Here is how I'm starting the process within my 64bit App
ProcessStartInfo p = new ProcessStartInfo("consoleapp.exe");
Process process = Process.Start(p);
process.WaitForExit();
I have already tried user impersonation and running the Process object as an administrator, and it isn't working.
The dll file, x.dll is definitely in the working directory of consoleapp.exe, as I have made the process do a console.writeline() on the working directory, and it matches where the x.dll file is located.
The SEHException error code is 0x80004005, which is E_FAIL "Unspecified Failure"
UPDATE: As requested, stack trace is:
at Consoleapp.Consoleapp.x(int32 var)
at Consoleapp.Consoleapp.Main(String[] args)
Not sure how that helps you. If you mean the stack trace of the External Exception, I can't work out how to access that (InnerException is null) - some kind of guide may be good to help me do that.
The message attached to exception is "External component has thrown an exception."
The x.dll file reads a file in a subdirectory under CSIDL_COMMON_APPDATA. This variable is correct when I run program directly, and from web app - used console.log on Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
The file contents are read to get database location and credentials to read from a particular database. If any part of that fails, the x.dll returns an error code, not an exception, and I have double checked this is the case by changing credentials in file, database name, record numbers, etc. and no SEHExceptions were thrown when run directly.
Upvotes: 4
Views: 868
Reputation: 332
Make it
public static extern x(int var);
and/or try to specify it as void
public static extern void x(int var);
Upvotes: 1