Link to home
Start Free TrialLog in
Avatar of ktola
ktola

asked on

DLLImport Not Working on Windows Server 2008 R2

I have a C++ DLL that I am attempting to use through a business class object and a DLLImport statement.  When I run the code locally on my Windows 7 Pro 64-Bit computer, everything works fine.  However when I run the exact same code on my production Windows 2008 R2 SP1 server, I get 400 Bad Requests returned to me.  I ended up using a debug tool and determined that IIS out in production is never calling this DLL and thus the errors must be to do with the DLLImport statement not retrieving the DLL.

I never registered the DLL on either my Windows 7 or Server 2008 box and, to be honest, I am using DLLimport statements because I am unable to directly reference the DLL in VS 2010.  I have run other DLLs in Production using DLLImport statements without any issue and I cannot figure out why this one is not working.

Any ideas?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

What type of project are you working with (WCF, REST Service, ...)?

Is this a COM DLL?
Avatar of ktola
ktola

ASKER

The DLL is not COM - it is a non-CLR C++ DLL thus the reason for the DLLImport

The code calling it is a WCF service using RESTful calls and JSON but none of that is the issue.  I can run everything locally with no issues
Since this a REST service, all errors on the server side are translated to 400 Bad Request, so you need to log the exception on the server side to get the details.  

You need that information in order to find the reason for the failure.  My guess is that it can't find the library from the DllImport attribute, or you don't have access rights to the file.
Avatar of ktola

ASKER

Unfortunately, I cannot trap the error as a DLLImport is an unmanaged call.  I tried adding the directory holding the DLL to the PATH; adding the DLL to the System32/SysWow64 directories and the InetSrv within each.  Nothing is working and I do not think the DLL is ever being called.
Unmanaged calls should still get an exception.  Are you catching the exceptions from the web service calls?  I have a WCF REST service where I log all calls, and all exceptions.
Avatar of ktola

ASKER

.NET only traps managed exceptions - things like DLLImport calls are not managed and cannot be trapped (I tried).  Worse yet nothing appears in the logs even though exceptions have to be thrown somewhere.  I know the issue is the fact that the DLL is not being called or accessed as it emits debug statements and the debug viewer for that DLL never activates when going through IIS.  If I run a console application on the server, the DLL works just fine and emits debug statements accordingly.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ktola

ASKER

That is not the issue - we now have an open Microsoft support ticket and, thus far, they have failed as well to figure out the issue.  The DLL is being found, there are no extraneous references, and yet the DLL is not being called in Production.  Local IIS works just fine.
Avatar of ktola

ASKER

I wanted to provide the solution in case anybody else has this use.  As it turns out, Application Pools in IIS run under there own Application Pool Identities.  If you are referencing any third party or external DLLs, you need to provide specific permission to this identity in order to get things to work.  Here is a link - http://learn.iis.net/page.aspx/624/application-pool-identities/
Avatar of ktola

ASKER

I appreciated your attempts to help...