Solved

Import unmanaged Dll to C# application

Posted on 2014-04-16
4
635 Views
Last Modified: 2014-04-16
I need to import a dll built in unmanaged code, to my C# application. The dll is placed in the same place as the executable. Since I don't know where the user will choose to install the application, I need to define the path to the dll at runtime. How can this be done?

[DllImport("MyImport.dll", CharSet = CharSet.Ansi)] gives DllNotFoundEexception (Exception from  HRESULT: 0x8007007E)

[DllImport(Application.StartupPath + "\\MyImport.dll", CharSet = CharSet.Ansi)] gives build errror "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"
0
Comment
Question by:JasonMewes
  • 2
  • 2
4 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40003816
>>The dll is placed in the same place as the executable.

If that is the case then the line:
[DllImport("MyImport.dll"
should find the dll, because searching the directory the exe is located in is one of the default locations the DllImport uses when it searches.

Check the dll is really in that folder and check the spelling (it should not be case sensitive).
0
 

Author Comment

by:JasonMewes
ID: 40004055
Thanks for your answer. I have now found the cause of the error. The import dll couldnt find one of it's dependencies, which caused the error. So in fact the error message was misleading.
0
 

Author Comment

by:JasonMewes
ID: 40004154
I've requested that this question be closed as follows:

Accepted answer: 0 points for JasonMewes's comment #a40004055

for the following reason:

The import dll couldnt find one of it's dependencies, which caused the error. So in fact the error message was misleading.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 40004112
OK, the error message was misleading BUT you asked about how to use DllImport where the dll is not in a fixed directory path - which I addressed in my comment.


The dll is placed in the same place as the executable. Since I don't know where the user will choose to install the application, I need to define the path to the dll at runtime. How can this be done?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now