# [.net] Using Dll's C#

This topic is 3860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello and welcome to my thread of confusion. OK so here is the deal. Learning C# and I'm more more C Flat than sharp. Translation is I can fumble my way around mostly but I'm still way out of my element on some things. For example getting the use of an exteral library or in this case dll. More specifically FreeImage. Now mostly I've been looking at this page as a place to start. The bottom most example compiles fine, doesn't show anything but nothing gets angry so I'm guessing everything is cool. As a starting point I have been trying to make a simple project that using the following FreeImage call then spits the string out. I figure once I can do that I'll be getting somewhere. DLL_API const char *DLL_CALLCONV FreeImage_GetCopyrightMessage(); Returns a string containing a standard copyright message you can show in your program. This leaves me with a kinda tangled mess of questions. First is getting things set up to use the DLL. There really isn't any kind of documentation for setting up FreeImage anywhere, they just say you shouldn't have any problems. I'm guessing the expect the user to know what the heck they are doing. They have a C# project that spits out a dll and they have a cs file that makes a lot DllImport calls so I guess its not part of that project but then again I'm CFlat so I cant say for sure. It gets real fuzzy around if I should use some form DllImport, or use the cs file which says something about needing to be compiled as unsafe. I'm also not 100% I get what all is happening with the DllImport thing. My interpretation is your saying there is a Dll named whateverDll and it has a function in it named getWhatever, and it takes these parameters. Which leads me the next question which is if the function returns a pointer to a char string how does that tie in with C# which has its own string thing going on? I may be moving too fast but I guess I feel if I can do xyz in C I expect to be able to do it in C# too.

##### Share on other sites
DLLImports (see Platform Invokes) are just a way of telling C# which function you want from which DLL, its prototype, and how to marshal its parameters.

[DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

In this case I'm importing GetProcAddress from kernel32.dll, telling it the character encoding of the strings used, and telling it not to mess with the function name (Windows API calls have A or W stuck on the end depending on the character encoding you're using, GetProcAddress does not). C# automatically handles marshaling just fine for most types, but in some cases you need to give it hints (like CharSet) to let it know how to expect the data. These can also be applied to individual parameters (with [MarshalAs(...)] string foo, ...).

It's a broad topic, so it's probably best to do some research on it yourself.

##### Share on other sites
Quote:
 Original post by RaIt's a broad topic, so it's probably best to do some research on it yourself.

It seems like a broad one but I was having trouble finding anything meaty on the subject. Searching for Platform Invoke seems to be getting me better information to look at, so thanks for the pointer.

*****edit

OK seems like I got the one function going with the following. It happly spits out a copyright string to the console.

using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;namespace ConsoleApplication1{    class Program    {        [DllImport("FreeImage.dll")]                public static extern string FreeImage_GetCopyrightMessage();        public static void Main()        {            string tester = FreeImage_GetCopyrightMessage();            System.Console.WriteLine(tester);            while(System.Console.KeyAvailable==false);        }    }}

[Edited by - Goober King on June 24, 2007 10:33:37 AM]

##### Share on other sites
Forgive me if you already know this and are just doing this as an exercise, but freeimage does come with .net bindings.

##### Share on other sites
Quote:
 Original post by gharen2Forgive me if you already know this and are just doing this as an exercise, but freeimage does come with .net bindings.

Well I know that now. It has a lot of this and that and it took me awhile to sort through it all. I've been using their .cs file for the most part for a couple of days now, After I basically started rebuilding the thing from scratch. I opened up the thing and realized not only could I understand it now but I was rebuilding a shoddy version of perfectly fine wheel. So I ditched my wheel and took their's.

That being said its still all one big exercise into the world C# and visual anything. Now I'm just trying to get my freeimage data to show up on a standard windows form, which I just accomplished to some degree. Though it took me most of the night to pull off.

But hey, at least I do have the Dll thing down.