Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Loops Section Overview
Suggested Courses

578 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