I want to use something similar to NativeLibrary.TryLoad for a native dll in dot net standard. But I cannot find a method for it. Is there a good way to do this?
I know a can do things like
[DllImport(#"alib.dll")]
internal extern static int func();
but that is not what I want in this case. Im tracking a bug that cause a library to sometimes not load (from inside a dll that Im calling). So I want to preload it in my program and see if that helps.
You can use LoadLibrary or LoadLibraryEx (https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw).
Related
I can't reference ImageSearch.dll in my project. I've been trying for days and can't get any further. it seems to me that i'm the only one with this problem and i don't know what to do next. Is it possible to reference a .dll manually, for example via lines of code? It's nerve wracking and need this or a similar feature.
I keep getting the following error:
Could not add a reference to imagesearch.dll. Make sure the file is accessible and is a valid assembly or COM component.
enter image description here
Hope someone can help me...
That message is telling you that the dll you're trying to reference is something that .NET doesn't know how to work with automatically. This means it has no idea what functions are in the dll or how they work. So, if a dll isn't a .NET assembly or exposed via COM, then you can use PInvoke (Platform Invoke).
Don't add the dll as a reference to your project at all, instead add it as a content file that gets output with the rest of your compiled code. Getting PInvoke to work with an arbitrary DLL can be quite complicated, so be prepared for some headaches. There's an entire website with examples of how to pinvoke for all sorts of libraries at http://pinvoke.net/ that will give you lots of ideas.
Then you can call methods in the dll by doing something like:
// Import ImageSearch.dll (containing the function we need) and define
// the method corresponding to the native function.
[DllImport("ImageSearch.dll"]
private static extern int FindImage(string imagePath);
Obviously I have no idea what imagesearch.dll is or does, so I have no idea what the actual PInvoke function should look like, you'll have to figure that out from the dll's interface.
https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke
I'm trying to monitor a running application written in C++ using a different C# application.
In my C++ code I have defined an API:
_declspec(dllexport) //is this even possible when compiling an .exe?
int getSomething();
Is there a way to call this function from the C# code?
Will the classic approach work:
[DllImport("myexe.exe", CharSet = CharSet.Auto)]
public static extern int getSomething();
Yes, any PE executable can export functions this way. Just keep in mind that the compiler will sometimes mangle the export names, resulting in stuff like this:
MyAPIFunction#16
You can check that the names are OK by loading the executable file into a tool such as PEInfo.
You should be able to call it in exactly the same way you would a function in a DLL.
Update
Ok, so it looks like you want IPC, not a P/Invoke call. See this page for info on how to use named pipes in C#. And here's a great place to start looking for info on how to use named pipes in C++.
Yes, you can export functions from a .exe exactly like you can from a .dll and the way you've shown is the correct way to do that.
No, you can't interact with an existing process by doing that, just as loading a function from a .dll wouldn't allow you to interact with other processes using that .dll.
I am working on a c# crawler/Poster project, it crawls wordpress blogs, sites, using WebClient to download content.
Wordpress sites use a bug or i don't know, of WebClient, for some reason it does not accept all cookies, from wordpress blogs, it may be a measure to stop auto bots, spammers.
So decided to use Sockets, but seems sockets also has a few problems, it sometimes does not return full response, so not reliable, but i found a good working code in VC++, i am trying to use it in C#, but i dont know vc++ at all.
Here is the code
How do i create a dll of the above code?
I have created a simple dll project using vc++ but unable include the above code in the project.
Updated Link to Code
You can use platform invokes, creating a declaration for each function you need. Here's an example of importing the MessageBox WinAPI function (note that this is not the same as the MessageBox class in .NET!)
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
Now that I have downloaded your code, I can say this...
Even if we got this code to compile, we still have to do the normal heavy lifting of interop between C++ and C#. And the code isn't even ready for that. The http_download.h file is a big set of inline classes. And the instructions for making C++ Code invokable from C# is to long to list here. You would basically have to get this code to compile as a DLL. Then from the DLL either export a set of "C" functions that invoke your C++ to do what you want. Or convert these C++ classes into COM objects with a type library.
But let's analyze what you are really trying to do:
You want to crawl web pages, but WebClient doesn't work.
So I think the real question you want to ask:
Why doesn't WordPress accept my cookies with WebClient?
And I really don't know the answer, because you haven't shared your WebClient code or elaborated what you think may be the issue. But I bet it's an easily solved problem.
I'm certain WebClient will be 10x easier to use than than some C++ code hacked up as one .h file that doesn't look very pretty. And it will be 100x time easier than trying to put together a HTTP library using pure sockets (which is what WebClient is, but it supports all the features you need, but haven't realized yet).
Sorry if I'm curt. I'm trying to encourage you to think about this a better way than doing it the hard way.
Create a class library by using the C++ code and add it as a reference to your C# project.
I have a C source code written in VC++6.0 and I need that code to be used in a C# application which I am coding.
I have done few things to make that happen, I have run that C source file in release mode and have generated the intermediate.manifest file and the other files there, can I use any of these file to make my problem solved.
What you want to do is import the dll like this:
[DllImport("user32.dll")]//This is where you identify your dll...
public static extern int MessageBoxA( //This would be an object from your dll
int h, string m, string c, int type);
public static int Main()
{
return MessageBoxA(0, "Hello World!", "My Message Box", 0);
}
The keywords that you need to search on to learn about this are "running unmanaged code" and "PInvoke".
Another alternative is to wrap the C code in managed C++. Microsoft calls it C++/CLI. Anyways, you then compile an 'assembly' from that wrapper. An assembly is just a DLL that contains managed code. Then you can 'reference' (the managed equivalent of linking) that 'wrapper' from a fully managed C# application.
So the dependency could look like this:
C -> Managed C++ -> C#
If you are comfortable enough wrapping up c code in c++ you could easily make a dll by following this microsoft guide. It details how to go about creating a dll in C++. If you need a more in-depth explanation though feel free to get back to me, and I will try and knock up a quick how-to.
Also let me know if you use a different IDE or environment and I can taylor the answer to suit your needs more precisely.
Might be later on today though as am pressed for time at the moment.
I have win32 DLL named VssSdkd.dll. It contains two classes with names VssSdkServiceLogin and VssSdkMsg.
I need to import the VssDskServiceLogin class, in C#. I am setting few properties of VssDskServiceLogin object and passing this to VssSdkMsg which in turn invokes some other method.
How can I achieve this using C#.
I have win32 DLL named VssSdkd.dll. It
contains two classes with names
VssSdkServiceLogin and VssSdkMsg.
In C#, I need to import the
VssDskServiceLogin class. In the class
are some attributes I need to set the
value for, sending the information to
VssSdkMsg and call another function
I need to achieve those things through
C# code. Is this possible, and if so,
how?
Classes compiled in C++ (and other Win32 languages) cannot be interoped with Dot NET languages. Structures may be if care is taken. Dot NET does have support for COM objects, though.
Native functions may be called from Dot NET languages if they're tagged with the [DllImport] attribute on the CLR side (and the appropriate DllImportAttribute properties are set) - and exported on the Win32 side. However, this is a non-trivial process. I would recommend grabbing a good book on the subject and starting from the top. SO is probably not a very good medium for addressing this issue.
You can do it with p/invoke and marshaling. Read about it, it's too complicated a subject to explain fully in a SO answer.
I believe it is sometimes possible in C# through P/Invoke, but when dealing with classes, this can get very, very difficult.
I'd highly recommend creating a C# wrapper for your DLL using Managed C++ instead.
I don't know that this link will solve your problem directly, but I expect you can find a good example of how to do this at Code Project. Managed C++ can be a little tricky until you get used to it, and I think the syntax changed from .NET 1.0/1.1 to .NET 2.0. Make sure you know what version of .NET you're targeting and search the Code Project site accordingly.
Possibly what you are looking for is interoperability with COM.
I haven't worked much on it, but can give you a sample code to start with.
[DllImport("user32.dll")]
private static extern int MessageBox(IntPtr hWnd, String
text, String caption, uint type);
static void Main(string[] args)
{
MessageBox(new IntPtr(0), "Hello, world!", "My box", 0);
}
This may be of help.