473,545 Members | 2,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

LoadLibrary not working on Vista64?

I have a class named DynamicLinkLibr ary which does this:

[DllImport("kern el32.dll")]
private static extern IntPtr LoadLibrary(str ing fileName);

protected virtual void Load(string fileName)
{
EnsureNotDispos ed();
if (LibraryHandle != IntPtr.Zero)
throw new InvalidOperatio nException("Lib rary already loaded");
if (string.IsNullO rEmpty(fileName ))
throw new ArgumentNullExc eption(fileName );

LibraryHandle = LoadLibrary(fil eName);
if (LibraryHandle == IntPtr.Zero)
throw new InvalidOperatio nException("Inv alid 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 5604
"Peter Morris" <su*****@NOdroo pySPAMeyes.comw rote in message
news:%2******** **********@TK2M SFTNGP04.phx.gb l...
>I have a class named DynamicLinkLibr ary which does this:

[DllImport("kern el32.dll")]
private static extern IntPtr LoadLibrary(str ing fileName);

protected virtual void Load(string fileName)
{
EnsureNotDispos ed();
if (LibraryHandle != IntPtr.Zero)
throw new InvalidOperatio nException("Lib rary already loaded");
if (string.IsNullO rEmpty(fileName ))
throw new ArgumentNullExc eption(fileName );

LibraryHandle = LoadLibrary(fil eName);
if (LibraryHandle == IntPtr.Zero)
throw new InvalidOperatio nException("Inv alid 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("kern el32.dll"), SetLastError=tr ue]
....
If (LibraryHandle == IntPtr.Zero)
// Call Marshal.GetLast Win32Error 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:

DynamicLinkLibr ary in DynamicLinkLibr ary.dll
FortranDynamicL inkLibrary in FortranDynamicL inkLibrary.dll
MySpecificDLL in MySpecificDLL.d ll (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*****@NOdroo pySPAMeyes.comw rote in message
news:e9******** ******@TK2MSFTN GP02.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.

DynamicLinkLibr ary in DynamicLinkLibr ary.dll
FortranDynamicL inkLibrary in FortranDynamicL inkLibrary.dll
MySpecificDLL in MySpecificDLL.d ll (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
61140
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 service cannot find the DLL in the same directory. This is probably because the DLL needs to be in a different directory, perhaps the same directory...
8
3614
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 in VC++ in command link.exe. This dynamically loaded library can then be loaded by function LoadLibrary(). The address of a function say func1(int)...
8
21227
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 GetProcAddress on to pass info to non-.NET apps. I can not use the standard "interop" process, as the DLL's are created dynamically. I need this: ...
3
6491
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 initially placed on the .NET 247 forum as such: http://dotnet247.com/247reference//msgs/28/140461.aspx I have expanded it here to let one call...
11
5268
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 invalid". If I use LoadLibraryEx(dll,NULL,DONT_RESOLVE_DLL_REFERENCES) it loads fine (returns non-null). I've looked in Dependency Walker and it...
3
3648
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 inhouse webserver running IIS 5.1 and it works correctly. I'm specifing a real path to LoadLibrary of the form C:\domains\...\file.dll (obtained...
1
10183
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 web service cannot find the DLL in the same directory. This is probably because the DLL needs to be in a different directory, perhaps the same...
2
3068
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 in the worker thread in the loaded library. Two questions 1). Does loadlibrary(0 run the executable in the same thread as the calee (probably...
1
3512
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 research, I gather that the Jet drivers are not available for 64 bit apps, but they do have a 32 bit Jet driver on Vista 64. So the solution I think...
0
7468
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7808
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7423
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7757
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5329
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.