Jason Wilborn

I'm using Visual Studio 2005 Standard Edition and I'm programming in C++. I'm running Windows XP. I created a class library and compiled it as a DLL and then I made a simple windows form application to link to and test the functions of the DLL. The DLL works fine with the application and I can compile both with no errors and the functions in the DLL can be called from within the application. The dll is referenced from it's compiled directory and I have no issues.

BUT the whole advantage to a DLL is to provide code for multiple programs and have it in 1 location and the only way I can run my executable is to have the DLL in the directory with the exe. If I have to copy the DLL to every program's directory that uses it then I'm wasting my time putting the code in a DLL as I'll still have to make sure I copy the DLL if I make a change to multiple directories.

Everything I've read states there is a search path for DLLs, but in my experience windows checks the directory of the exe and just stops. Nothing I try works. The online help on the subject stresses the search order which isn't working. I just get an error saying the DLL isn't found regardless if I put it in the winnt, winnt/system or winnt/system32.

I found a reference to using the PATH enviroment variable so I added all the DLL search locations that windows is suppose to be using to my C++ directories option dialog page in VS2005 but still no luck.

What steps do I have to do to get windows to search the DLL path search order when the application calls the DLL



Re: Visual C++ Language Location of a DLL

einaros

See SetDllDirectory: http://msdn2.microsoft.com/en-us/library/ms686203.aspx.

Also note that such questions are handled by other forums (specifically http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=980773&SiteID=1), not this one.






Re: Visual C++ Language Location of a DLL

Jason Wilborn

First off I apologize for posting in the wrong place. I seem to never get the "right" forum based on the way I look at things. I am using C++ and having an issue with windows coded using C++, so when I scan the forum topics C++ sounds right. If I posted in the SDK forum I'm certain someone there would have said I'm using C++ so post here.

Second, thanks for the link to the article, but I must apologize as I must be asking the wrong question or wording my question wrong. That article repeated many other microsoft articles and help pages that keep saying there is an order of locations that windows looks for a DLL. System32 is always listed yet when I put a DLL there it says it can't find it. SetDLLDirectory, if I'm understanding the function's intent will just add another place for windows to look. But if I want to just drop the DLL in System32 I don't see why I'd have to add code to specify System32. I guess my question should be worded, how do I make windows, from within my application when it calls a DLL, do what all the microsoft help pages say it's suppose to do Or maybe someone can tell me what part of the search order I'm misunderstanding because when I read it, I'm taking away an understanding that windows is going to actually look in those locations. Does it or does it not Or does it search those locations only if you tell it to





Re: Visual C++ Language Location of a DLL

einaros

Jason Wilborn wrote:

Second, thanks for the link to the article, but I must apologize as I must be asking the wrong question or wording my question wrong. That article repeated many other microsoft articles and help pages that keep saying there is an order of locations that windows looks for a DLL. System32 is always listed yet when I put a DLL there it says it can't find it. SetDLLDirectory, if I'm understanding the function's intent will just add another place for windows to look. But if I want to just drop the DLL in System32 I don't see why I'd have to add code to specify System32. I guess my question should be worded, how do I make windows, from within my application when it calls a DLL, do what all the microsoft help pages say it's suppose to do Or maybe someone can tell me what part of the search order I'm misunderstanding because when I read it, I'm taking away an understanding that windows is going to actually look in those locations. Does it or does it not Or does it search those locations only if you tell it to

It does search those locations, that's a safe bet. The problem is likely to be caused by other factors.

How exactly are you loading the dlls Also, did you include a manifest with them






Re: Visual C++ Language Location of a DLL

Jason Wilborn

I followed the instructions of several sources, partly from a thread on this forum when I ran into trouble using DLLs with Windows Forms applications and from the help topics from VS2005. I created a Class Library where I coded my functions as part of a wrapper class. I compiled that to make my actually DLL. In a windows form application I included a #using statement followed by the "dll_name". In the project options for resolve #using references I listed the directory of the release version of the DLL I compiled. So basically I'm calling the DLL implicity if my understanding of the help articles is right. I can use the functions from my DLL within the application no problem. It compiles fine even if the DLL isn't located in the application's working directory.

But when I debug from within VS2005 or double click the exe the program loads and runs fine until I reach the point where the program calls 1 of the functions from the DLL. At that point it tries to load the DLL and if it's in the same directory it works flawlessly. If it's in any other directory listed in the search order I get a JIT error listing that the DLL wasn't found.





Re: Visual C++ Language Location of a DLL

einaros

With dlls loaded through references, your best bet is perhaps to load it into the global assembly cache (GAC). Is that an option to you

The second alternative may be to copy the assembly to the CLR system directory, but that doesn't seem like very good practice, and may not even work. Try the GAC, then head for other options.






Re: Visual C++ Language Location of a DLL

Jason Wilborn

I'll read up on the GAC and try it. I'm willing to try anything that'll work and learn something new.



Re: Visual C++ Language Location of a DLL

Jason Wilborn

einaros wrote:

With dlls loaded through references, your best bet is perhaps to load it into the global assembly cache (GAC). Is that an option to you

The second alternative may be to copy the assembly to the CLR system directory, but that doesn't seem like very good practice, and may not even work. Try the GAC, then head for other options.

Thanks. The GAC worked once I read what I had to do. At least I know how to get it to show up using the .net framework.





Re: Visual C++ Language Location of a DLL

Simple Samples

Jason Wilborn wrote:
First off I apologize for posting in the wrong place. I seem to never get the "right" forum based on the way I look at things. I am using C++ and having an issue with windows coded using C++, so when I scan the forum topics C++ sounds right. If I posted in the SDK forum I'm certain someone there would have said I'm using C++ so post here.
No, you definitely would not be told to go to a C++ forum or newsgroup just because you are using C++ to do Windows programming. C/C++ is /are the langauge(s) that has been the traditional one for Windows programming.

If you are not sure of what forum or newsgroup to use, then post a question in Where Is the Forum For...






Re: Visual C++ Language Location of a DLL

Jason Wilborn

einaros wrote:

See SetDllDirectory: http://msdn2.microsoft.com/en-us/library/ms686203.aspx.

Also note that such questions are handled by other forums (specifically http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=980773&SiteID=1), not this one.

I got it to work with the GAC but was trying to expand on what I could do and is there something special you have to do to use SetDllDirectory or LoadLibrary in a windows form application I followed the instructions on the link and included windows.h and it tells me the identifier is not found.

Edit: I actually got LoadLibrary to be recognized but the example from the help listed the parameter type wrong. I just happened to try LPCSTR instead of LPCTSTR which is what the example said and it worked. Still can't get SetDllDirectory to even be recognized when including windows.h. I looked in the winbase.h where it's defined and maybe I'm looking at it wrong but it appears to me that the #if is set up to make the function unuseable.





Re: Visual C++ Language Location of a DLL

einaros

Jason Wilborn wrote:

Still can't get SetDllDirectory to even be recognized when including windows.h. I looked in the winbase.h where it's defined and maybe I'm looking at it wrong but it appears to me that the #if is set up to make the function unuseable.



You will probably have to define WIN32_WINNT 0x501, to compile for XP+.