References and Environmental Variables

Started by
7 comments, last by myvraccount 9 years, 11 months ago

I'm trying to do various things in .NET which require DLLs and similar libraries to be imported, and environmental variables to be set. Honestly, I never had to do this very much, and it's been a long time, and now I'm using Visual Studio 2012, and the interface is a bit different. Here are my problems:

- I can't find where/how to set environmental variables. I could even hard-code them into a file if I need to, if someone tells me exactly what file and what to put where, but there should be a way to do it easier through the IDE.

- I can't seem to add DLL references. I have some projects that already have references in them, but I don't remember how I added them. Now it seems that if I click the references from the menu bar, it doesn't have a browse button in any of my new projects, and sometimes the references option isn't even on the menu at all! I don't know if this depends at all on what type of project I start (empty, Windows app, etc.), or what language I use, or any other considerations, but it's very inconsistent.

I want to ultimately be using C#, but I think I need to start with C++ for compatibility with the DLLs, then make a wrapper to use in C#. Also, I've no experience doing that, so if anyone has any info about it, it would be appreciated.

Thanks.

Advertisement

What do you mean "evnironmental variables"? Are you referring to the system environment variables? They're generally set from the System Properties dialog on the control panel. If you want to to write them programmatically well LMGTFY.

As for references, right click on "references" under your project in the solution explorer and click "add reference". If you're still having trouble with that, come back with a more specific question.

Finally, there are several ways to use a C++ lib/dll, but IMO, the best way is to use C++/CLI to wrap the library and provide CLR classes that can be used from C# (F# or VB.net or whatever other .Net language you like)

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

I guess I thought there was a way to set the environmental variables somewhere in the project settings. It hadn't occurred to me to do it programmatically, but I'll test that, and if it works, I'll just do it that way.

That's how I was trying to add references, but when I click the "Add New Reference" button, a window pops up which is supposed to have a list of DLLs or LIBs, but it's empty, and I have no way of browsing in that or the previous window. The problem is inconsistency between projects. Some projects have certain menu options, and buttons and lists available in certain windows, and other projects do not, and I can't see why that would be.

As for wrapping C++ for C#, I guess what I'm trying to do is: once I have the libraries I need imported into a C++ project, I want to make new functions for a better interface, and one that I can compile into a DLL that is compatible so I can import it into C#. Basically I'm trying to use code that was written in C++ and intended to be used in C++, and make a wrapper so I can use it in C#, but I've never done anything like that before, so I don't know how.

Thanks for the info btw.

First off, it's environment variables, second, why don't you just put the dlls in the project folder directory?

Which folder exactly? The one with the .sln, .suo and .sdf files, or the one with the .vcxproj, .filters and .users files?

If I put it in the right folder, will it show up on the list and then I don't need to browse anyway?


Which folder exactly? The one with the .sln, .suo and .sdf files, or the one with the .vcxproj, .filters and .users files?

Im not exactly sure since i generally uncheck the "create directory for solution" box when i create new projects. I think you can find it by going to the project configuration dialog, in project->project property->configuration property->debugging->working directory. You should see $(ProjectDir) in the box, click the browse... option and you should see what directory it use.

When deploying the program, it's a lot simpler, just drop the dlls in the same folder as the executable, it will try to find them there first. If it can't find them it will check the windows and windows/system32 directory, then the environement variables.

Well I finally got it to import and recognize a DLL reference (which wasn't easy! Jesus, Microsoft, can't you make a consistent interface?!) but then of course when I try to call a function I get a compilation error that says the DLL is corrupt or something, which I know isn't true, because I just downloaded it fine, and it's widely used, so there can't be a problem with it. Also, it comes with a sample program that uses the DLL and doesn't have any errors.

I haven't gotten around to trying to set the environment variable yet, because I can't get the DLL to work anyway. Also, I worry about whether the problem may be that the environment variable is not set, and that's causing it to somehow not recognize the DLL (I'm not sure exactly why the environment variable does, or for that matter, what they're supposed to do in general), but if this is the case, I'd need to set the environment variable before runtime, and the runtime function would not be applicable.

To set an environment variable, go to Control Panel/System and click on Advanced System Settings, then on the Environment Variables button. If you can't figure out what to do from there, programming is probably not for you :)

As for the dll, what dll is it? Is it a /net assembly or a native dll? Do you have all the dependant dlls?

Google "dependency walker" and load the dll into that, it will show you if you're missing anything.

Don't take this the wrong way, but you're asking some pretty basic questions. There's nothing wrong with that, but you might want to start with an easier project.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

Oh, yeah I've seen that on the system properties page, but I didn't realize I was supposed to set the environment variable for the whole computer, I thought it was just for the project.

As far as I know it's a regular compiled DLL, because it's intended to be used in C++, but I'm using it in C++ .NET, though I'd like to wrap it so I can use it in C#.

I'll try that. Unfortunately I don't think I'll have time to work on it any more until the weekend, as I have other obligations.

I know you don't mean any thing condescending, and I know you're trying to help (which you are doing BTW and I appreciate it) but let's get something strait. I'm not "starting" with any project; I've been writing code for over 20 years, in everything from C++ to .NET to Java to Lisp to Prolog to Eiffel for God's sake. I've done AI, compression algorithms, cryptography and just about anything else you can think of. I don't meant o brag but you get my point, right? It's just that usually I don't have to rely on anything third party, beyond the built-in code libraries for a language. I don't think I've ever had to set an environment variable, and I haven't used DLLs since earlier versions of .NET, in which the interface was different, and frankly I'm not entirely sure I remember how I did it. And for some reason, the stupid .NET interface is inconsistent (I get different menu options in different projects, and the windows have different buttons). Also, I don't know why, but whenever I try to search for relevant info, especially for the specific DLLs I'm using, I don't find anything useful. If I have a noticeable flaw, it's that I'm not good at finding info online. For some reason, I search and fail to find anything useful, which is why I came here. Usually it's not a big deal though, because most of what I typically do is just plain logical/mathematical programming, where all I have to understand is the pure language itself and deductive reasoning - nothing more, which is the way I like it, but in this case, I have to rely on other means.

Anyway, I'm not entirely convinced that the DLL isn't somehow corrupt, because it said there was an error using it (I don't remember the error off hand), but I don't see how it could be corrupt, since I downloaded it successfully from a widely used public website, so that makes me think that maybe I'm doing something wrong, despite the fact that I included it into my project supposedly correctly.

This topic is closed to new replies.

Advertisement