PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Compact Framework DLL Hell: How do I P/Invoke "own" DLL?

Reply

DLL Hell: How do I P/Invoke "own" DLL?

 
Thread Tools Rate Thread
Old 21-06-2007, 12:04 PM   #1
Benjamin Lukner
Guest
 
Posts: n/a
Default DLL Hell: How do I P/Invoke "own" DLL?


Hi!

I've written a DLL using eVC++ 4.0 that ships with our devices and is
used by a VB.Net "system" program that automatically starts on boot.
HKLM\Loader\SystemPath includes the directory of the DLL.

The DLL is also used by a VB.Net application. Now I updated the DLL and
the application and put both into the same directory.

Problem: When the application P/Invokes the dll the file from the system
directory is taken instead of the file from my own directory!

Q: What is the syntax for a VB.Net 2003 CF P/Invoke to tell it to use
the dll from the own directory?

Kind regards,

Benjamin Lukner
  Reply With Quote
Old 21-06-2007, 12:39 PM   #2
Christian Resma Helle
Guest
 
Posts: n/a
Default Re: DLL Hell: How do I P/Invoke "own" DLL?

On Jun 21, 1:04 pm, Benjamin Lukner <Ben...@web.de> wrote:
> Hi!
>
> I've written a DLL using eVC++ 4.0 that ships with our devices and is
> used by a VB.Net "system" program that automatically starts on boot.
> HKLM\Loader\SystemPath includes the directory of the DLL.
>
> The DLL is also used by a VB.Net application. Now I updated the DLL and
> the application and put both into the same directory.
>
> Problem: When the application P/Invokes the dll the file from the system
> directory is taken instead of the file from my own directory!
>
> Q: What is the syntax for a VB.Net 2003 CF P/Invoke to tell it to use
> the dll from the own directory?
>
> Kind regards,
>
> Benjamin Lukner


<DllImport("[YOUR DLL]")> would also work for DLL's located in your
applications working directory. The syntax for P/Invoke in VB.NET
would be something like this:

<DllImport("coredll")> _
Private Shared Sub LocalFree(handle As IntPtr)


--
Regards,
Christian Resma Helle
http://christian-helle.blogspot.com

  Reply With Quote
Old 21-06-2007, 03:25 PM   #3
Benjamin Lukner
Guest
 
Posts: n/a
Default Re: DLL Hell: How do I P/Invoke "own" DLL?

Christian Resma Helle wrote:

>>Q: What is the syntax for a VB.Net 2003 CF P/Invoke to tell it to use
>>the dll from the own directory?


> <DllImport("[YOUR DLL]")> would also work for DLL's located in your
> applications working directory. The syntax for P/Invoke in VB.NET
> would be something like this:
>
> <DllImport("coredll")> _
> Private Shared Sub LocalFree(handle As IntPtr)


That's exactly the problem: IT DOESN'T! :-(

I use (what's an "old style" alternative declaration):

Private Declare Function SDC_WrapperVersion Lib "sdc_wrap.dll" _
( _
) As Int32

sdc_wrap.dll is located in a directory that SystemPath points to and
returns 1.
sdc_wrap.dll is also located in the directory of my exe and returns 2.

But when running my application I always get 1, so the wrong dll is used.

Kind regards,

Benjamin Lukner
  Reply With Quote
Old 21-06-2007, 07:16 PM   #4
Guest
 
Posts: n/a
Default Re: DLL Hell: How do I P/Invoke "own" DLL?

You tried a fully qualified path to the DLL in your P/Invoke declaration?
The loader looks in \Windows first, so the behavior is expected.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com


"Benjamin Lukner" <BenLuk@web.de> wrote in message
news:OB%238G%23AtHHA.484@TK2MSFTNGP06.phx.gbl...
> Christian Resma Helle wrote:
>
>>>Q: What is the syntax for a VB.Net 2003 CF P/Invoke to tell it to use
>>>the dll from the own directory?

>
>> <DllImport("[YOUR DLL]")> would also work for DLL's located in your
>> applications working directory. The syntax for P/Invoke in VB.NET
>> would be something like this:
>>
>> <DllImport("coredll")> _
>> Private Shared Sub LocalFree(handle As IntPtr)

>
> That's exactly the problem: IT DOESN'T! :-(
>
> I use (what's an "old style" alternative declaration):
>
> Private Declare Function SDC_WrapperVersion Lib "sdc_wrap.dll" _
> ( _
> ) As Int32
>
> sdc_wrap.dll is located in a directory that SystemPath points to and
> returns 1.
> sdc_wrap.dll is also located in the directory of my exe and returns 2.
>
> But when running my application I always get 1, so the wrong dll is used.
>
> Kind regards,
>
> Benjamin Lukner



  Reply With Quote
Old 22-06-2007, 10:45 AM   #5
Benjamin Lukner
Guest
 
Posts: n/a
Default Re: DLL Hell: How do I P/Invoke "own" DLL?

<ctacke/> wrote:
> You tried a fully qualified path to the DLL in your P/Invoke declaration?
> The loader looks in \Windows first, so the behavior is expected.


Yes, I've read about the search order of dlls.
And no, I didn't try the fully qualified path.
Because the application (and dll) can be located in any folder on the
device.
But DllImport says "Constant expression is required."

How do I fill a constant with a value that I only know by the time my
application starts?

Kind regards,

Benjamin Lukner
  Reply With Quote
Old 22-06-2007, 12:09 PM   #6
Ginny Caughey [MVP]
Guest
 
Posts: n/a
Default Re: DLL Hell: How do I P/Invoke "own" DLL?

Benjamin,

Did you try renaming your own DLL to a different name so there wouldn't
exist a DLL with the same name in the \windows folder?

--
Ginny


"Benjamin Lukner" <BenLuk@web.de> wrote in message
news:OPtVfGLtHHA.2004@TK2MSFTNGP03.phx.gbl...
> <ctacke/> wrote:
>> You tried a fully qualified path to the DLL in your P/Invoke declaration?
>> The loader looks in \Windows first, so the behavior is expected.

>
> Yes, I've read about the search order of dlls.
> And no, I didn't try the fully qualified path.
> Because the application (and dll) can be located in any folder on the
> device.
> But DllImport says "Constant expression is required."
>
> How do I fill a constant with a value that I only know by the time my
> application starts?
>
> Kind regards,
>
> Benjamin Lukner


  Reply With Quote
Old 22-06-2007, 12:48 PM   #7
Benjamin Lukner
Guest
 
Posts: n/a
Default Re: DLL Hell: How do I P/Invoke "own" DLL?

Ginny Caughey [MVP] wrote:

> Did you try renaming your own DLL to a different name so there wouldn't
> exist a DLL with the same name in the \windows folder?


I don't want to rename it because it is only a newer version of the
other dll.

The only thing I'd like to know is if there is a chance to P/Invoke a
dll in my own directory if there's another one in the Windows folder...

Kind regards,

Benjamin Lukner
  Reply With Quote
Old 22-06-2007, 03:08 PM   #8
Guest
 
Posts: n/a
Default Re: DLL Hell: How do I P/Invoke "own" DLL?

You can't. What you should do is make sure the DLL is in the FILES and not
MODULES section of the platform's BIB file, then you can just replace it in
the \Windows folder. Unfortunately the CF provides no other mechanism.

-Chris



"Benjamin Lukner" <BenLuk@web.de> wrote in message
news:OPtVfGLtHHA.2004@TK2MSFTNGP03.phx.gbl...
> <ctacke/> wrote:
>> You tried a fully qualified path to the DLL in your P/Invoke declaration?
>> The loader looks in \Windows first, so the behavior is expected.

>
> Yes, I've read about the search order of dlls.
> And no, I didn't try the fully qualified path.
> Because the application (and dll) can be located in any folder on the
> device.
> But DllImport says "Constant expression is required."
>
> How do I fill a constant with a value that I only know by the time my
> application starts?
>
> Kind regards,
>
> Benjamin Lukner



  Reply With Quote
Old 22-06-2007, 07:17 PM   #9
Robert Simpson
Guest
 
Posts: n/a
Default Re: DLL Hell: How do I P/Invoke "own" DLL?

"Benjamin Lukner" <BenLuk@web.de> wrote in message
news:ejzizN$sHHA.2752@TK2MSFTNGP06.phx.gbl...
> Hi!
>
> I've written a DLL using eVC++ 4.0 that ships with our devices and is used
> by a VB.Net "system" program that automatically starts on boot.
> HKLM\Loader\SystemPath includes the directory of the DLL.
>
> The DLL is also used by a VB.Net application. Now I updated the DLL and
> the application and put both into the same directory.
>
> Problem: When the application P/Invokes the dll the file from the system
> directory is taken instead of the file from my own directory!
>
> Q: What is the syntax for a VB.Net 2003 CF P/Invoke to tell it to use the
> dll from the own directory?


Call the LoadLibrary() API specifying the path of your DLL before calling
any of your imported functions from that DLL. I think if the DLL is already
loaded in your app space before .NET attempts to load it (the first time you
make a call into it), it will use the library already in memory.

Robert


  Reply With Quote
Old 22-06-2007, 08:44 PM   #10
Guest
 
Posts: n/a
Default Re: DLL Hell: How do I P/Invoke "own" DLL?

An interesting and worthwhile thing to test. Let us know if that works.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com


"Robert Simpson" <rmsimpson@noemail.noemail> wrote in message
news:OSjzbmPtHHA.1208@TK2MSFTNGP05.phx.gbl...
> "Benjamin Lukner" <BenLuk@web.de> wrote in message
> news:ejzizN$sHHA.2752@TK2MSFTNGP06.phx.gbl...
>> Hi!
>>
>> I've written a DLL using eVC++ 4.0 that ships with our devices and is
>> used by a VB.Net "system" program that automatically starts on boot.
>> HKLM\Loader\SystemPath includes the directory of the DLL.
>>
>> The DLL is also used by a VB.Net application. Now I updated the DLL and
>> the application and put both into the same directory.
>>
>> Problem: When the application P/Invokes the dll the file from the system
>> directory is taken instead of the file from my own directory!
>>
>> Q: What is the syntax for a VB.Net 2003 CF P/Invoke to tell it to use the
>> dll from the own directory?

>
> Call the LoadLibrary() API specifying the path of your DLL before calling
> any of your imported functions from that DLL. I think if the DLL is
> already loaded in your app space before .NET attempts to load it (the
> first time you make a call into it), it will use the library already in
> memory.
>
> Robert
>
>



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off