473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

LoadLibrary not working on Vista64?

I have a class named DynamicLinkLibrary which does this:

[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string fileName);

protected virtual void Load(string fileName)
{
EnsureNotDisposed();
if (LibraryHandle != IntPtr.Zero)
throw new InvalidOperationException("Library already loaded");
if (string.IsNullOrEmpty(fileName))
throw new ArgumentNullException(fileName);

LibraryHandle = LoadLibrary(fileName);
if (LibraryHandle == IntPtr.Zero)
throw new InvalidOperationException("Invalid DLL file name: " +
fileName);
FileName = fileName;
}
In WinXP this works fine, however, when I run it on Vista the result of
LoadLibrary is always IntPtr.Zero. Using a relative path (same folder) or
absolute path makes no difference. Can anyone help? This is stopping me
from migrating an app to Vista!
Thanks
Pete
Oct 21 '07 #1
4 5594
"Peter Morris" <su*****@NOdroopySPAMeyes.comwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
>I have a class named DynamicLinkLibrary which does this:

[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string fileName);

protected virtual void Load(string fileName)
{
EnsureNotDisposed();
if (LibraryHandle != IntPtr.Zero)
throw new InvalidOperationException("Library already loaded");
if (string.IsNullOrEmpty(fileName))
throw new ArgumentNullException(fileName);

LibraryHandle = LoadLibrary(fileName);
if (LibraryHandle == IntPtr.Zero)
throw new InvalidOperationException("Invalid DLL file name: " +
fileName);
FileName = fileName;
}
In WinXP this works fine, however, when I run it on Vista the result of
LoadLibrary is always IntPtr.Zero. Using a relative path (same folder) or
absolute path makes no difference. Can anyone help? This is stopping me
from migrating an app to Vista!
Thanks
Pete

If you're trying to load a 32bit library, you'll need to target the "X86
platform" when compiling your code.

Note that you should check your error code when the LoadLibrary API call
fails.

[DllImport("kernel32.dll"), SetLastError=true]
....
If (LibraryHandle == IntPtr.Zero)
// Call Marshal.GetLastWin32Error to get the win32 error code.

Willy.

Oct 21 '07 #2
Hi Willy

Thanks for the answer!

I didn't know that I cannot use LoadLibraryEx in Win64 to load a 32 bit dll!
This is how my app is structured, each project depends upon the one above:

DynamicLinkLibrary in DynamicLinkLibrary.dll
FortranDynamicLinkLibrary in FortranDynamicLinkLibrary.dll
MySpecificDLL in MySpecificDLL.dll (dynamically loads 3rdparty.dll)
MyApp.exe

I've now set the Target Platform of MyApp.exe to x86 and it works perfectly.
Are there any negative points to doing this?

Thanks very much for your help, I really appreciate it!
Pete
Oct 21 '07 #3
"Peter Morris" <su*****@NOdroopySPAMeyes.comwrote in message
news:e9**************@TK2MSFTNGP02.phx.gbl...
Hi Willy

Thanks for the answer!

I didn't know that I cannot use LoadLibraryEx in Win64 to load a 32 bit
dll! This is how my app is structured, each project depends upon the one
above:
You can not load a 32bit DLL into a 64bit process. A Managed application
built with AnyCPU (MSIL) as target, will load the 64bit CLR and as such run
as a 64bit process.

DynamicLinkLibrary in DynamicLinkLibrary.dll
FortranDynamicLinkLibrary in FortranDynamicLinkLibrary.dll
MySpecificDLL in MySpecificDLL.dll (dynamically loads 3rdparty.dll)
MyApp.exe

I've now set the Target Platform of MyApp.exe to x86 and it works
perfectly. Are there any negative points to doing this?
No, your program runs as a 32bit process under Wow64 on 64bit Windows. The
performance impact of the transition into the 64bit kernel is extremely low,
and is completely compensated by the lower CPU cache pressure.
That means that your code in general runs as fast as it would be compiled as
64 bit.

Willy.

Oct 21 '07 #4
Excellent, thankyou so much for your help!
Oct 21 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Siegfried Heintze | last post by:
I have some C functions I need to expose as XML web services. My original plan was to deply an XML Web service in C# and use P/Invoke to call my C functions. This is not working because the web...
8
by: one2001boy | last post by:
Hello, I can use call a function with any arugment from LoadLibrary(), but not a function with argument of "FILE*. For example, I can build a .DLL dynamically loaded library with option /DDD...
8
by: ATS | last post by:
HOWTO Implement LoadLibrary\GetProcAdrress\FreeLibrary in C# Please help, I want to fully implement LoadLibrary\GetProcAdrress\FreeLibrary in C#, and be able to call functions that I use...
3
by: ATS | last post by:
HOWTO Implement LoadLibrary, GetProcAdress, and FreeLibrary. Below is code that I want to be able to use simple LoadLibrary\GetProcAddress\FreeLibrary technqiues on. I've used the code that was...
11
by: tomrmgc | last post by:
I have a dll that was working in VC6, but isn't loading after being built in VC71. LoadLibrary() returns null and the error code (from GetLastError()) is -2147483645 "One or more arguments are...
3
by: Michael Tissington | last post by:
I'm using LoadLibrary to import a DLL in a asp.net application. The dll was written in c++ and is located in the bin folder I have been testing the website on my development machine and our...
1
by: Siegfried Heintze | last post by:
I have some C functions I need to expose as XML web services. My original plan was to deply an XML Web service in C# or VB and use P/Invoke to call my C functions. This is not working because the...
2
by: Bit byte | last post by:
I am writing an application that requires me to load a (Win32 C) libray and then register several call back functions. I am loading the libary in a worker thread and then registering functions...
1
by: Figmo | last post by:
I have an app that needs to access local MDB files using OLEDB. I have just upgraded to Vista64 and when I run this app I get a "OLEDB. 4.0 is not registered on local machine" error. From...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.