Saturday, May 9, 2009

How do I reference a dll in C# correctly?

There is this RCTDLL that I want to add as a reference to my project. But when I do add it to my project I get the error "A reference to C:\WINDOWS\WinSxS\x86_Microsoft.Windows.... cannot be added. Please make sure that the file is accessible, and that is a valid assembly or COM component"

How do I reference a dll in C# correctly?
First you need to know if this is a .net dll file, a COM component or a native win32 dll file.





You can easily add a reference to .net and COM dll files from References-%26gt;Add





If it's a native dll then you need to know which function you want to import and the prototype of the function (the name, parameter list and their types, return type).


You can use a tool such as Depends.Exe located


Visual Studio .NET Folder\Common7\Tools\Bin


which will list all the exported functions for native dlls. However, it doesn't give you the list of parameters! So you need the documentation for the dll to be able to use it's functions.


For example:


the lstrcat function in kernel32.dll would be imported like this:


[DllImport("kernel32.DLL")] private static extern int lstrcat


([MarshalAs(UnmanagedType.LPTStr)] string lpstring1,


[MarshalAs(UnmanagedType.LPTStr)] string lpstring2);
Reply:I think the dll is not a COM dll so you won't be able to add reference to it:


You can use dllImport to invoke the methods present in that Dll.


For this you must be knowing the signature of the methods that you want to call,


Go through the following link.





Calling Win32 DLLs in C# with P/Invoke


http://msdn.microsoft.com/msdnmag/issues...





I think this will solve your problem.


If still there for further help you can mail me,


i'll try to help if i can


No comments:

Post a Comment