Reading Dll parameters - c#

I have a DLL file with four functions, I need to get access to the four functions but they don't have names, just entry points which I know. I have to figure out what the four functions do.
The question is, how do I call these unknown functions in C#(.Net)? And how can I figure out what their parameters and return values are?
The DLL is Sndvolsso.DLL . It's functions are nowhere on the internet.(unless you can find them) If anyone knows of a tool I can use to read parameters and return values, that would be very helpful.

If your DLL is unmanged:-
Using IDA
Disassemble the dll and find the exported functions.
Double click that function and its declaration will be shown in IDA.
An Example of MessageBoxA of user32.dll in IDA:-
.text:77D6EA11
.text:77D6EA11 ; int __stdcall MessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
.text:77D6EA11

You can also use the free version of IDA 5.0.

Parameter / type / signature information is not stored in unmanaged DLL files. You can get the address of each exported function using LoadLibrary and GetProcAddress, and using ordinal. But all you get is the address; it's up to you to know how to call the functions appropriately.
It would help you to understand how functions work at the binary / machine code level and how the stack works.
Here are your options:
As you said, find information from external sources like the internet.
If you can find the PDB file, this is the debugging information file, and it's how debuggers know how to associate locations in memory to lines of code. It will contain all the metadata you're looking for. But in your scenario I don't think you have access to this.
If you are comfortable with assembly and understanding executable code at the binary level, disassemble or debug the DLL. See what it's expecting, how it uses the stack.
Otherwise you will have to compromise:
Use a different library. Most practical, because honestly if you don't know anything about this sndvolsso.dll, then even if you get it "working", you will be lost once any problems arise.
Hire someone who can do this.
Just write your own code instead of using this one.
But come to think of it, your question doesn't really make sense. If you don't know what these functions are, how do you know you need them?

Related

As a C# developer, how do I find out whether a DLL method needs to be P/Invoked with stdcall or with cdecl calling convention?

Consider the following two (randomly chosen, just because I happen to use both of them in the project that I have currently open) example API calls, both contained in Microsoft libraries:
LocalDBCreateInstance
OpenPrinter
The first one needs CallingConvention.Cdecl when P/Invoked through the DLLImport attribute, the second one needs the default value WinApi/StdCall.
Currently, I use one of the following techniques to find out which one to use:
Guess: If it's built-in Windows API, it's probably StdCall, otherwise it's probably Cdecl.
Research: Find the SDK containing the header file, install the SDK, search my whole hard drive to find that header file buried somewhere deep inside some obscure folder, and then look inside and check the function declaration.
Is there an easier way? For example, is there some reliable way to deduce the calling convention from the MSDN documentation link of the API method (see the two examples above)?
Checking the header files is the right thing to do.
Step 1: Find out which header file you need. Both examples you mentioned have information about the required header file on their documentation page (msoledbsql.h and Winspool.h, respectively).
Step 2: Get the header file. You can do this by installing the SDK containing the header file, but, as you point out in your question, it can be a hassle to find the SDK, download it, find its installation location, etc.
However, chances are that you are not the first one to use that API method and that the header file can be found in a public repository. Querying Sourcegraph, for example, for LocalDBCreateInstance msoledbsql.h and OpenPrinterW file:winspool\.h$ will yield the relevant information as the very first result.
HRESULT __cdecl LocalDBCreateInstance(PCWSTR wszVersion, PCWSTR pInstanceName, DWORD dwFlags)
BOOL WINAPI OpenPrinterW(LPWSTR pPrinterName, LPHANDLE phPrinter, LPPRINTER_DEFAULTSW pDefault);
Other alternatives for C# developers include:
Download the Win32 metadata file and find the correct C# DllImport specification with ILSpy.
Use the C#/Win32 P/Invoke Source Generator to auto-generate Windows API declarations in your code.
Those are official Microsoft projects, so the information is likely to be accurate. Note, though, that they will work only for built-in Windows API methods (so they would help with OpenPrinter but not with LocalDBCreateInstance).
(Kudos to #The2Step and #Simon Mourier for teaching me about Sourcegraph and win32metadata/CsWin32 in the comments.)

How generate a DLL from C program for use in a C# project

I need to use a program made in C for a C# project , and for this I need to generate a DLL in Visual Studio 2013, anyone have any tutorial or know how to do it?
I want make a DLL for a structs and read/write functions in C, cheers guys.
You have to use p/invokes. I think this is very often discussed here. Take a look at: Access a method from a DLL from C# program
DLL is probably overkill for what you are doing. You can just write a C procedure and P/Invoke it using CDecl. The problem is that you need to know a whole lot for this. If you are simply trying to iterate over an array fast for a performance critical section, you are better off using structs in C#, slapping unsafe on your method, and then using pointers and addresses to do what you want to do. Code that looks a whole lot like C/C++ is perfectly legal in C#.
See a reference here: MSDN : Unsafe Pointers
Also fished out a somewhat dated reference showing how to P/Invoke the visual c Runtime printf function as an example. Keep in mind that things get really hard when you need to give a pointer to a function and when you need to read offsets etc. to pass around structs. You'll need to pin anything you pass into the method to stop it from being moved by the garbage collector, which will also have performance implications.
MSDN: CDecl P/Invoke example

Can you extract dll version numbers from bundeled dlls

I need to bone up on my CLR compiling knowledge, so i'm going to speak in generalities... Appologies if I'm not being specific enough.
I'm working on an application that references a COM Library dll which has a number of dlls rolled into it. My question is, is it possible using Reflection to get a reference to the sub dll's assembly, namely to obtain the version number?
Or, if this question makes no sense, i'll try to rephrase.
No can do, you've got no way to find out what DLLs the COM server might be using. There isn't anything like Assembly.GetReferencedAssemblies() in unmanaged code. Process.Modules is about as close as you can get but there's no way to find out that the module you'll iterate was actually associated with the server instead of being loaded by, for example, OpenFileDialog or some obscure virus scanner crap.
If you know the DLL names and where they are located then you can use the FileVersionInfo class to obtain their unmanaged version resource info.

P/Invoke dynamic DLL search path

I have an existing app which P/Invokes to a DLL residing in the same directory as the app itself.
Now (due to the fact that Canon produces one of the crappiest API's around) I need to support two versions of this API and determine at run-time which one I should use (old or new). Since the DLLs have the same name (the first one loads other DLLs with same names so just renaming the first one won't help me) I must keep them in different directories.
Hence my question: what options do I have to control what directory the DLL given in a DllImport declaration uses?
I guess I can start out by trying any of these two ideas:
1) Use "SetDllDirectory" to set my desired directory before doing the first P/Invoke and then reset it afterwards.
2) Load the desired DLL manually using "LoadLibraryEx" and hope that that will do the trick.
But are there any more ".NET:ish way" to try out first?
UPDATE: I realize that I can stuff all access to the DLLs in two separate .Net assemblies and then place each one of them in a separate directory with the corresponding API files. Then I can load the proper .Net assembly dynamically and the loading of the correct DLL whould happen automatically. Any reason that shouldn't work?
I can think of one: how would I go about debugging this stuff? It is possible to tell Visual Studio that an assembly (contained in my solution) shall be placed in a subdirectory and debugged from there?
My condolences, I've seen one of the APIs and it was indeed shockingly bad. The bigger problem is that you'll need to be able to convince Windows to find the DLL. They won't be in your .exe directory so the default won't work. Using SetDllDirectory() would work, using Environment.CurrentDirectory does too. LoadLibrary cannot work, the P/Invoke marshaller will use LoadLibrary itself.
If it is at all an option, you can use different names for the two P/Invoke declarations, using different arguments for the DllImport() constructor and using the EntryPoint attribute. Doesn't sound like that will fly.
I think 2nd option will work, but it will require to write a lot of code to manage dll loading in .net.
First one might work also, but i either don't like it.
Here is my suggestion: you can specify full path (and may be relative) in DllImport [DllImport(#"C:\dll\a32.dll"]
Your first option (P/Invoke with SetDllDirectory) is the option I personally prefer. Unfortunately, there isn't a ".NETish" way to handle loading native DLLs...which does make sense.
Starting with .NET Core 3.0, and also works with .NET 5 & .NET 6, you can use NativeLibrary.Load(string) to dynamically load DLLs at runtime, and use via P/Invoke normally.
Check this answer for more details: https://stackoverflow.com/a/69958827/211672

Where can I find a reference explaining how to take a C/C++ function header and define it with dllimport in C#?

I am using setupapi.dll to determine USB device connectivity. It isn't working properly in 64 bit versions of windows (XP, Vista). I suspect that the declarations are not quite correct, but am not sure how to verify. MS doesn't seem to provide the dllimport information, but they do list all the function definitions. Is there a resource I can use to convert between the function header and the dllimport declaration?
I've used pinvoke.net, but it looks like it doesn't account for 64 bit in the definitions provided.
Check out pinvoke.net. It has a large set of Win32 APIs already translated to DllImport definitions (Including setupapi.dll).
The tool you're looking for is called C++/CLI. It was built to process those headers and generate interop code - no mucking around with P/Invoke.
Write a light-weight wrapper assembly in C++/CLI, and then use it from your C# code.
(Hard to provide a more specific answer, since you didn't really specify how it's not working properly...)

Categories

Resources