Solved

Import unmanaged Dll to C# application

Posted on 2014-04-16
4
665 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…

615 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