Solved

Import unmanaged Dll to C# application

Posted on 2014-04-16
4
626 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…

757 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

22 Experts available now in Live!

Get 1:1 Help Now