Why does this work in a Windows Forms solution but not a as Class Library?
July 21, 2010 2:37 PM   Subscribe

I have a driver written in C++; it's set up as a solution within a VS 2008 project. If I go and create a new "Windows Forms" solution and reference the driver, it works fine (Intellisence works, it compiles etc). But when I try to do the same exact thing with a "Class Library" solution it will not see,link, whatever anything in the driver. I know I'm light on information, but its that simple "Class Libary"->Fail Winforms-> "No Fail"! I can't seem to come up with any good google searches for this either. Please help me hive mind!
posted by rickim to Computers & Internet (14 answers total) 1 user marked this as a favorite
 
Can you post the error message that you're getting? Do you get the error when you attempt to add the reference, or when you attempt to compile the class library? Assuming you can add the reference, what does the Error List show?
posted by a snickering nuthatch at 2:50 PM on July 21, 2010


Response by poster: Thanks for the response!

I get the error when trying to compile the class library and its when I'm calling the constructor:
"A get or set accessor expected".

It sees the namespace and class but no methods or anything beyond that.
posted by rickim at 3:20 PM on July 21, 2010


That's not a C# error, not a C++ error. Are you creating a Visual C# class library project or a Visual C++ class library project? More to the point, which one do you mean to be using?
posted by jedicus at 3:52 PM on July 21, 2010


argh. That is a C# error; not a C++ error.
posted by jedicus at 3:52 PM on July 21, 2010


Response by poster: Eck. Sorry for the omission of info.

Class Library is in C#.

Thanks again.
posted by rickim at 3:57 PM on July 21, 2010


Response by poster: ...and functioning Windows Form solution is also in C#.
posted by rickim at 3:59 PM on July 21, 2010


Well, okay, so you're trying to invoke C++ code from your C# project. Are you doing all of the setup required for this (DllImport and so forth)?
posted by jedicus at 4:08 PM on July 21, 2010


Response by poster: No, I am not doing DllImport. I was under the impression that it was for libraries (dlls) whereas I'm trying to use a solution where one portion is in C++ and the other is in C#.

It does work with the Windows Forms solution without any of the dllimport stuff. Is there some intrinsic difference between the two (C# Class Library vs. C# Windows Forms)? If I need to do DllImports is the Windows Forms solution automatically taking care of this somehow?

Worse case is I can just use the Forms solution and remove all the superfluous stuff, but thats very hacky.

Thanks again.
posted by rickim at 4:22 PM on July 21, 2010


I can't pull up VS right now, but I believe when you create a "Class Library", you need to specify the the language (maybe as an item in the sidebar?) the project is written in. It sounds like you've set it up so that it's finding C++ when it expects to find C#.

Hopefully someone comes along with all the answers, but if that doesn't happen, what might help me would be some screenshots of where you're creating the project, and then the panel where it lists the projects and the files in them (but again, hopefully someone comes along and fixes this better than me.)
posted by !Jim at 4:31 PM on July 21, 2010


In my last job, I did driver development in C++ on windows. You can only use Visual Studio as an editor and a frontend for the command line compiler in the WDK (Windows Driver Kit). You need to set up the project to invoke the command line (nmake.exe) for its build steps instead of the default C# one (which is probably how the .sln one is set up). check out the MSDN intro documentation here for more info. here. It's never going to work as a C# project since .Net code cannot run in the kernel anyway.
posted by theDrizzle at 8:35 PM on July 21, 2010


oh wait, just reread the question...are you trying to link a .SYS driver file into your C# code like you would with a .dll? that's not going to work since the driver runs in kernel-mode and is loaded separately by Windows. You need to make some sort of ioctl to call into the driver from userspace.
posted by theDrizzle at 8:40 PM on July 21, 2010


Your question doesn't make sense as you wrote it rickim - do you really mean a driver, as in something that installs a device driver? My limited experience says you normally communicate with a device driver through the DeviceIoControl() call and don't link to it directly (as well as theDrizzle being correct - normally you have to compile drivers using the WDK which you would know if you were using by the mental scars).

If you just mean a library or a dll, then things are simpler. If it's a normal DLL you need to use P/Invoke as jedicus said. If it's a C++ library you cannot call it directly, and either have to recompile it in "managed C++" (which is unlikely to work unless it is very simple) or create a DLL so you can use P/Invoke.
posted by samj at 1:37 AM on July 22, 2010


samj: "normally you have to compile drivers using the WDK which you would know if you were using by the mental scars."

Indeed! I have those scars and I have vowed to never go back to kernel land ever again. Two years was enough for me!
posted by InsertNiftyNameHere at 2:09 AM on July 22, 2010


Response by poster: I am very sorry for the confusion, and truly appreciate your answers.

The vendor calls it a driver, I guess its really not. Its a bunch of C++ classes and methods without a main(). I am going to look into the managed C++ thing, failing that its PInvokes for me. Still a bit confused on why the forms solution works, though.
posted by rickim at 6:05 AM on July 22, 2010


« Older Batman theme song meme   |   How can pork become less filling? Newer »
This thread is closed to new comments.