Jason Wilborn

I'm new to Visual Studio (I have VS 2005 Standard Edition) and I'm using C++. I'm trying to learn how to code a DLL with some standard functions I use instead of including a cpp file into every project that needs it. Using the help I got a DLL to work with a Win32 project using __declspec(dllexport) and __declspec(dllimport) and by using a .def file. I know the DLL itself works.

When I try a Windows Form Application I run into problems. I follow the same steps for linking to the DLL but I get errors as it can't use __declspec(dllimport). From the help files in Studio I gather because Forms apps compile using CLR:Pure I can't import the usual way, but none of the examples seems to explain what you do to use the DLLs. I've tried .def files and I get the same thing. When I call the function I get this.

Linking...
WF_Test_App.obj : error LNK2028: unresolved token (0A00001D) "int __clrcall WF_Test_App::fnBasic_DLL(void)" (
fnBasic_DLL@WF_Test_App@@$$FYMHXZ) referenced in function "public: __clrcall WF_Test_App::Form1::Form1(void)" ( 0Form1@WF_Test_App@@$$FQ$AAM@XZ)
WF_Test_App.obj : error LNK2019: unresolved external symbol "int __clrcall WF_Test_App::fnBasic_DLL(void)" (
fnBasic_DLL@WF_Test_App@@$$FYMHXZ) referenced in function "public: __clrcall WF_Test_App::Form1::Form1(void)" ( 0Form1@WF_Test_App@@$$FQ$AAM@XZ)
C:\Code_VS\WF_Test_App\Debug\WF_Test_App.exe : fatal error LNK1120: 2 unresolved externals

Using that info and looking through the help I know what my problem is but no where have I found what to do instead. I'm probably looking in the wrong place. In a Windows Form project what should I be doing to correctly import in the functions from the DLL to make use of them

Thanks



Re: Windows Forms General Using DLLs in a Windows Form Application

Chris.getUniqueId

I've got the same problem.

I've heard people mention that you need to have wrappers of unmanaged code call the DLL, but don't know what this means.

Why on earth do they make this so complicated What happened to the satisfied people they show us during the lengthy install

I spend the majority of my time dealing being microsofted instead of actually doing the heart of the code I am trying to write.





Re: Windows Forms General Using DLLs in a Windows Form Application

Jason Wilborn

Yeah I get bits and pieces but can never find somewhere that just lists full examples from the exe side and the DLL side. I've gathered from more research I should be making use of the #using command and referencing the DLL file, but I'm not sure what changes to make to the DLL and if I should compile it using clr pure like the forms application. And not sure what form the export should take to create the .lib file. I've tried various combinations and so far no luck.



Re: Windows Forms General Using DLLs in a Windows Form Application

SharonGuinee

Jason Wilborn wrote:

I'm new to Visual Studio (I have VS 2005 Standard Edition) and I'm using C++. I'm trying to learn how to code a DLL with some standard functions I use instead of including a cpp file into every project that needs it. Using the help I got a DLL to work with a Win32 project using __declspec(dllexport) and __declspec(dllimport) and by using a .def file. I know the DLL itself works.

When I try a Windows Form Application I run into problems. I follow the same steps for linking to the DLL but I get errors as it can't use __declspec(dllimport). From the help files in Studio I gather because Forms apps compile using CLR:Pure I can't import the usual way, but none of the examples seems to explain what you do to use the DLLs. I've tried .def files and I get the same thing. When I call the function I get this.

Linking...
WF_Test_App.obj : error LNK2028: unresolved token (0A00001D) "int __clrcall WF_Test_App::fnBasic_DLL(void)" (
fnBasic_DLL@WF_Test_App@@$$FYMHXZ) referenced in function "public: __clrcall WF_Test_App::Form1::Form1(void)" ( 0Form1@WF_Test_App@@$$FQ$AAM@XZ)
WF_Test_App.obj : error LNK2019: unresolved external symbol "int __clrcall WF_Test_App::fnBasic_DLL(void)" (
fnBasic_DLL@WF_Test_App@@$$FYMHXZ) referenced in function "public: __clrcall WF_Test_App::Form1::Form1(void)" ( 0Form1@WF_Test_App@@$$FQ$AAM@XZ)
C:\Code_VS\WF_Test_App\Debug\WF_Test_App.exe : fatal error LNK1120: 2 unresolved externals

Using that info and looking through the help I know what my problem is but no where have I found what to do instead. I'm probably looking in the wrong place. In a Windows Form project what should I be doing to correctly import in the functions from the DLL to make use of them

Thanks

Hi Jason,

So I haven't tried this yet, but I'm wondering if what needs to happen is adding a reference In the Solution Explorer window of the project that's going to call the dll, if you right click on References, then click on Add Reference... a window comes up with several tabs including Browse; the default file type for the browse is Component files, including dll's. I intend to try this out, but I may not get around to it for a while, so I thought I'd mention it in case it helps.

Sharon





Re: Windows Forms General Using DLLs in a Windows Form Application

Dav? S. A???????

http://forums.dcomproductions.net/viewtopic.php t=59

You can use the same concept to add functions instead of variables, as long as the functions are public static.





Re: Windows Forms General Using DLLs in a Windows Form Application

Jason Wilborn

Thanks for the link to what turned out to be the best explaination yet. Following those instructions with 1 modification I was able to successfully use a DLL created as a class library in a windows form application. The one modification was in the instructions it says to put the #using after the #pragma region statement. I got an error when doing that as the compiler said the imports could only be brought over globally. By moving the #using statement out of the form's namespace it worked fine. I took that knowledge and modified a Win32 DLL project and instead of using exports made use of /clr to get that DLL to work also.

Now I only have 1 question. The instructions said you could just put the DLL in the directory of the exe and that does work. But when I first starting messing with DLLs I read that they could be placed in the exe's directory or the windows system directory as both were checked by default for DLLs when called. I want to create some DLLs that will contain code used by several programs and having to copy a DLL anytime it's changed to each program's directory defeats the purpose of having a DLL doesn't it Or am I misunderstanding when or how a DLL is loaded from the windows system directory

Thanks 





Re: Windows Forms General Using DLLs in a Windows Form Application

Dav? S. A???????

Yes you can put them in the system folder. Or the GAC (Global Assembly Cache). (not sure where GAC is)

I'm glad my tutorial helped, if you ever need any help, don't be afraid to post on my forums, I don't mind helping out. :)





Re: Windows Forms General Using DLLs in a Windows Form Application

Jason Wilborn

After rereading my post I realize I left out a key sentence. I started with I read you could put them in the windows system directory but when I do that and don't put them in the exe's directory I get an error when running the program that it can't find the dll. Any suggestions.



Re: Windows Forms General Using DLLs in a Windows Form Application

Dav? S. A???????

Try making your DLL COM visible.





Re: Windows Forms General Using DLLs in a Windows Form Application

Dav? S. A???????

Just on a side note, the real system directory is not C:\Windows\, its C:\Windows\System32\.  A lot of people think Windows is, but its not.




Re: Windows Forms General Using DLLs in a Windows Form Application

Jason Wilborn

Yeah I've tried system32 and system and get the same error. I'm reading up now on making the DLL com visible as I've never done that and need to figure it out. Then I'll try again.

Thanks