loadlibrary - location problem

Posted on 2005-03-31
Medium Priority
Last Modified: 2013-11-20
I have a VB6 dll wrapper that wraps around an ocx that wraps around a non-com windows dll. Although the ocx and non-com dll sit in the same folder, the ocx cannot load the non-com unless it (the non-com dll) is placed in system32.

Why is this, and how can we force the ocx to look for the non-com in the same folder as the ocx?

A few details:
- Both the ocx and the non-com are vc++ and were not written by me, but by another programmer at my company
- I asked the programmer to put ".\" in his loadlibrary in the ocx, hoping it would see the non-com in the same location - didn't work
- when the non-com is in system32 it works, otherwise the ocx is unable to load the non-com and it fails
Question by:SweatCoder
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

Assisted Solution

rcarlan earned 1000 total points
ID: 13677433
Do this in your OCX (I'm assuming it's implemented using MFC, if not, you'll have to obviously change the code to eliminate the MFC dependencies):

CString sDLLFilename = _T("NonComDll.dll");     // replace this with the file name of your non-COM DLL

CString sPathname;      // full path and file name of the current module (OCX)
VERIFY(::GetModuleFileName(NULL, sPathname.GetBuffer(MAX_PATH), MAX_PATH));
int nDirEnd = sPathname.ReverseFind(_T('\\'));
sPathname = sPathname.Left(nDirEnd+1) + sDLLFilename;


Needless to say you'll have to add error checking to the code. Also, ReverseFind(_T('\\')) is not necessarily the best way to extract the directory component, but I thought it would suffice for an example.

LVL 48

Accepted Solution

AlexFM earned 1000 total points
ID: 13678535
VB6 API calls are implemented using LoadLibrary function which searches for executable in the following directories:
The directory from which the application loaded (exe file and not ocx server).
The current directory.
The system directory.
The Windows directory.
The directories that are listed in the PATH environment variable.

Directory where ocx is placed in not part of this list. You can try to add required directory using SetDllDirectory API. I am not sure this will help, but it is easy to check. Another way is to set current directory (SetCurrentDirectory API) before API call. Or ensure that Dll is in one of directories where LoadLibrary can find it.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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