PLC - PC communication using Snap7 in C# WPF app - c#

please, I have a question about snap7.dll library. So long time I wanst be working in C#, so maybe I am doing something wrong. But is possible to use snap7 in C# WPF project as library or it was developed only for windows forms? Its stupid I know, but I am asking because I am not able to add snap7.dll into my project references. Thank you.

Downloaded and played around with the examples from http://snap7.sourceforge.net/
Looks like the console application doesn't reference the assembly directly.
This isn't a WPF vs Winforms thing. It is a managed vs unmanaged code thing.
There is a snap7.net.cs .net wrapper class file.
It references "snap7.dll" and exposes its functionality as a C# class. At runtime it will load the assembly using DllImport.
Copy both the snap7.net.cs file and the snap7.dll into your project.
Use the snap7 class methods/attributes in your code. Then update snap7.dll to copy to output directory, or use a post build event to copy the snap7.dll to your output directory.
EDIT: I want to restate you do NOT add reference to the snap7.dll directly using project -> references. The DllImport annotation of the wrapper class file will load it at runtime.

Related

how to create a dll file including a method in c#

Imagine I have different methods written in c#. And these are included in the main class now and work completely fine. But I have a plan to add an upadter function to this project. In this case my idea is to include each function in a DLL file with the function name. So my updater function can easily replace the old DLL file with the new if there's a new version available. The problem is I don't know how to create a DLL file by just including a method.
You shouldn't make a seperate .dll for each function. When writing the library I suggest adding a new project to the current solution of the type C# class library. Now you can easily use your newly written library inside the project you are using it for. Like this:
in the solution explorer you see a library project and a windows forms application project. The windows forms application uses the library's code to work. You can use the library when it's finished and compiled to a .dll by referencing it in the other project(s) under 'add reference'

Converting WebService into DLL

I have one windows service project. I want to convert that web service project into DLL. As a class library I can simply reuse those methods just by referencing that DLL.
I just want to know is there any simple way to do it or I have to manually create one class library project and reuse my code.
You can reference exe in other projects same as dll.
But moving this to separate class library is the best idea.
Just Click on the class library with the service code and select build. it will create a DLL file in your Debug folder in your project bin directory. You can use that DLL file anywhere.Hope this helps..
Just copy your code to class library project and built it. It will create .dll and you can easly use it by adding reference to your project. I think that would be a simplest approach
After searching and asking questions on ASP forums proper way of doing this is to manually reuse code.

Unable to load DLL 'UNRAR.DLL': The specified module could not be found

I just downloaded UnRAR.dll from http://www.rarlab.com/rar_add.htm package includes C# sample project when I run in VS 2010 after selecting .rar file for extract I am getting below error message
and when I add reference unrar.dll I am getting below error
Please help / point me what I am missing?
UnRAR.dll is not a .NET assembly so it cannot be used as a project reference.
It is a native DLL so you'll have to use P/Invoke to access its methods or write a C++/CLI wrapper around it.
There's some sample code in this MSDN forum thread that should provide a decent starting place and show the necessary approach.
There also appears to be a fairly complete wrapper included in the Blue Mirror CD/DVD Indexer project.
It should also be noted the SevenZipSharp includes support for this out-of-the-box. I haven't used it myself, but it might be worth checking into.
The sample you are talking about use
[DllImport("unrar.dll")]
to access the functions inside the library.
This DLL should be visible when the test project runs.
So you should copy it into the BIN\DEBUG folder or in a
folder where your project runs.

My C# and DLL Data Woes

I'm trying to see if it is possible to pull data from a DLL. I did some research and found that you can store application resources within a DLL. What I couldn't find, was the information to tell me how to do that. There is a MS article that explains how to access resources within a satellite DLL, but I honestly don't know if that is what I'm looking for. http://msdn.microsoft.com/en-us/library/ms165653.aspx I did try some of the codes involved, but there are some "FileNotFoundExceptions" going on.
The rest of the DLL information is showing up: classes, objects, etc. I just added the DLL as a resource in my Visual Studio Project and added it with "using". I just don't know how to get at the meat of it, if it is possible.
If dlls are .net, you can use reflection.
Using System.Reflection;
....
Assembly A= Assembly.LoadFrom(YouDllFileName);
string[] STA;
STA= A.GetManifestResourceNames();
// STA contains all the resource names in the dll
...
// to extract them
Stream str= A.GetManifestResourceStream(STA[I]);
// then, you can make that stream became a file if you wish
You can also extract a .net assembly resources by using ildasm
I'm not totally sure what you might be running into based on your description, but a couple of general pointers...
If what you are trying to find is files you've added to the project you do this:
Right click on the resource in solution explorer, hit properties and set the "Build Action" to "Embedded Resource".
For strings and icons, add a .resx file to the project and insert them in there. If that's what you're doing and still running into issues, check the Build Action.
There is two types of dll.
Managed dll - dll that writen on any .net language (like csharp)
The link that you provide is working with managed dlls.
Unmanaged dll - classic c/cpp dll.
in this case you need to link between managed (your code) and unmanaged.
To find what the type of your dll, you need to add this dll as reference.
In visual studio open project, right click on references(in Solution Explorer).
Then "add reference"->browse-> add your dll.
Then at references, you can see your dll.
Right click on him, and add view in Object Browse.
If you see something like class "c" inside namespace "b", you working with managed dll.
In Object Browser you can learn a lot about your dll (maybe this is more important, than just extract resources)
At this point you can do the way that "Daniel Dolz" answer to you.
Since you say you are able to add the DLL in a using directive you can probably use the methods that the DLL exposes. If you do not have the documentation for the DLL then you may just have to try using the object browser to see what it has to offer.
assume:
using MyDll;
you should them be able to call the methods like this:
string x = MyDll.SomeType.GetValue();
is that what you were asking?

ref and library C++/C#

I am working on C# for a few mounths. Mainly I am working on C++.
On C++ using the visual studio if I wanted to add a a static library I could add it using the configuration of the project add the header and lib and path.
When using C#, I think it is something like DLL, all those assemblies are complied on late binding ?
In addition using the visual studio for the C# I can add a reference. Is this the equel thing as I wrote in the beginning of the question ? only for something like DLL ?
If your want use a Win32 DLL in C#, you must write a C# wrapper for it.
C# is managed, and everything you reference as an assembly gets linked at runtime, as with DLL's in C++. The compiler will check at compile time that you are using your assemblies properly, but it wont link them yet. You can use http://research.microsoft.com/en-us/people/mbarnett/ILMerge.aspx to merge assemblies together, as a post-build step.

Categories

Resources