Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Import unmanaged Dll to C# application

Posted on 2014-04-16
4
Medium Priority
?
677 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…

722 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