Solved

Import unmanaged Dll to C# application

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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…
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…

752 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