calling c# dll with (service) references from python - c#

This question is similar to this one Including a service reference from a class library.
In my case I work with python and C#. In my C# ClassLibrary Project I use a service reference and a dll which provides a specific client as a class, used for calling the webservice. This code works fine if I try it in my console application:
class ws_class
{
[DllExport("webservice", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static string TestExport()
{
//Calling request
return response;
}
}
Now I want to use the .dll in python using this code:
api = ctypes.cdll.LoadLibrary(path)
api.webservice.retsype = ctypes.c_wchar_p
print(api.webservice())
In other cases this code also works fine for receiving string returned by a C# Class Library.
But as soon as I use the .dll reference for the client in my C# code, it doesn't work anymore. It seems like python cannot handle this issue.
Regarding the question I mentioned above, the config files must be identical but still it won't work.

Related

can´t connect using dll from windows service

I have a windows form application which is working fine. I created now a windows service with FileSystemWatcher, it went ok.But when I copied the code from the windows form I had before it doesn´t work. (It is working on the windows form)Is there anything I need to take care working with windows services?.The application (windows forms) is to receive a file, make some operations with that information and then using an dll, comms with a scale.
[DllImport("mydll.dll")]
public static extern int OPENETHNET();
[DllImport("mydll.dll")]
public static extern int ETHWRITE(string in1);
[DllImport("mydll.dll")]
public static extern int ETHREAD(StringBuilder in2);
[DllImport("mydll.dll")]
public static extern int CLOSEETHNET();
and then I use it like this:
string inputStatus = "050";
StringBuilder resultStatus = new StringBuilder(1024);
openRest = OPENETHNET();
if (ETHWRITE(inputStatus) == 1)
{
ETHREAD(resultStatus);
}
CLOSEETHNET();
It doesn´t even opens the conection..
I tried to change the account to LocalService and the other options but, still the same.. Any ideias?
EDIT:
I saw the post Call to native DLL fails from a .NET Windows service ANd It seems to be the same problem
I tried the other solutions but it didn´t worked So I´ll try the last option, that it will be to to "wrap the dll in a web service that will run on start up and then call through to that web service from the web service."
Can anyone explain me how to do that?
After a lot of tests I realised that problem was in the dll file, so I drop the windows services and implement a winform ( where I know that the dll file works) and use the winform like a service, I now this solution is not the best one but, well is working..

Possible bug (?) xsockets.net console server

I'm trying to start development on xsockets.net service.
My server doesn't want to recognize any of CustomController implementation for a Console Application (server). Everything works fine in Web project.
I've noticed that XSocketPlugins property contains a list of plugins. My plugin is not on a list for console App and is registered for Web.
The source code is simple like in ReadMe.txt
using (var container = XSockets.Plugin.Framework.Composable.GetExport<IXSocketServerContainer>())
{
container.StartServers();
container.OnServerClientConnection+=container_OnServerClientConnection
Console.ReadLine();<br/>
}
Defining new controller
public class CustomController1 : XSocketController
{
public void OnMessage(...)
{
//do stuff
}
}
I'm able to connect to Generic controller using C# client and JS client.
Any ideas?
Env: Windows7 64-bit, VS2012, .NET 4.0
Yes, the bug is described here: known-issues and the work around is also described.
Regarding the comment, please post the code you use to connect with the Client API and maybe I can see something in there. There should not be any problems connecting from the clients API.

Creating New Webservice with Available methods in DLL

I have a solution file ( i.e DLL file). i want to create webservice/WCF Service which exposes the methods of DLL file. so that other team can use webservice instead of DLL reference
simply we cannot Add DLL as a reference to another project because another project is using in java..
so i have been provided DLL file and asked me to create one webservice( WCF is also fine) by using DLL file related methods.
please help me and my question is how can i expose DLL methods in Newly created webservice?
webservice/wcf any thing should be fine .
You can try creating a WCF Service, which has reference to this DLL file, You can call the functions in DLL from Operation contract() in your service contract.
And these operation contracts can be called from your other java project.
Of course you can!
If you can't edit the DLL:
Just create a normal webservice solution, and create the web methods that you want to expose in the DLL.
Then just call the appropriate DLL method in each web method.
If you can edit the DLL, just turn the project into a webservice project and expose the appropriate methods
This Beginners tutorial is excellent and should get you pointed in the right direction.
You can expose all dlls from the WebService Application. Add reference to WebService project
[WebMethod]
public bool CheckLogin(string username, string password)
{
bool status = false;
SqlCommand Command = new SqlCommand();
try
{
Command.CommandText="Select count(*) from CM_Users where username='"+username+"' and passwd='"+password+"'";
Command.Connection=DbConnection.OpenDbConnection();
// this is Assembly Loaded from Application
int count=(int)Command.ExecuteScalar();
if(count>0)
status=true;
else
status=false;
DbConnection.CloseDbConnection(Command.Connection);
}
catch (SqlException expmsg)
{
DbConnection.CloseDbConnection(Command.Connection);
}
return status;
}

Mono for Android or Java and WCF

I am currently using Mono for Android to develop a mobile application. I have been trying to consume a WCF web service by adding a web reference to it but I can't seem to make the call that way. I am now considering to bite the bullet and rewrite the code using Java which I am not as good at as I am with C#.
I have 2 questions:
How do I consume a WCF webservice using Mono for android.
If I am to use java how would I call a method that looks like the one below:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/MyMethod",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
void MyMethod(CustomObjectFromDataContract c_Object);
When I make the call I get a MessageBox that says Unhandled exception System.Net.WebException:. When I step into the code I see that the error happens when you call
[System.Web.Services.Protocols.SoapDocumentMethodAttribute ("http://tempuri.org/IMyService/MyMethod", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public void MyMethod([System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] CustomObjectFromDataContract c_Object) {
this.Invoke("MyMethod", new object[] {
c_Object});
}
The invoke is the one throwing the exception.
I have resolved the issue. Here are the steps to make this work:
1. The service has to be a RESTful service
2. Instead of referencing Localhost (which I was in the generated code) use the IP address of the hosting machine. I think this is because Android runs in Dalvik VM which I suspect has a different local host from the one my Dev computer is using.
I did that and my service works now.
If you add a Web Reference, some references classes including a client should be generated.
You can then create an instance of the generated client and call MyMethodon the client.
So assuming you are using Visual Studio simply right click you MonoDroid project > Add Web Reference and enter the URL to your WCF service.
To call it you can do the following:
I have added a reference to a service with namespace Example.WebReference
I would then call it in the following way:
Example.WebReference.ServiceElement client = new Example.WebReference.ServiceElement();
var output = client.MyMethod(parameter);
Hope this helps.

Communicating between C# and Visual C++ over IPC port (OR: Sharing a type between a Visual C++ DLL and a C# application)

I have a DLL written in Visual C++ and an application written in C#. The application that is written in C# already uses IPC between multiple instances of itself so that it only ever runs one instance (it attempts to start an IPC server, and if it can't assumes there's already one running, in which case it sends the command line arguments over IPC to the existing application).
Now I want to send commands from a Visual C++, however, even when I create a type definition in Visual C++ that matches the one in C# (on an implementation level), it rejects the connection because they are fundamentally still two different types (from two different assemblies).
I thought about using Reflection in Visual C++ to fetch the type from the C# assembly, but I can't do that because then I'd have to ship the assembly along side the DLL (which defeats the purpose of the DLL being an API to the application).
I'm not sure of any other way I could really do it, other than store the class in yet another DLL and make both the application and the API DLL reference the class in that, but this is also not an ideal solution as I'd like a single API DLL to distribute.
Are there any suggestions as to how I can connect over IPC (other forms of communication like TCP are not permitted) to send requests to the application?
The solution was to place the InterProcess class in the API DLL and simply make the C# application use the DLL as a reference to bring in the class.
It is also important to note that in order to initialize the shared object correctly, I had to initialize the server side of the sharing in a separate AppDomain and make the C# application a client like so (this is a new version of the previous paste):
try
{
// Set up an interprocess object which will handle instructions
// from the client and pass them onto the main Manager object.
this.m_ServerDomain = AppDomain.CreateDomain("roketpack_server");
this.m_ServerDomain.DoCallBack(() =>
{
// We must give clients the permission to serialize delegates.
BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider();
serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
IpcServerChannel ipc = new IpcServerChannel("roketpack", "roketpack", serverProv);
ChannelServices.RegisterChannel(ipc, true);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(API.InterProcess),
"InterProcessManager",
WellKnownObjectMode.Singleton);
});
// Now initialize the object.
IpcClientChannel client = new IpcClientChannel();
ChannelServices.RegisterChannel(client, true);
this.m_InterProcess = (API.InterProcess)Activator.GetObject(
typeof(API.InterProcess),
"ipc://" + name + "/InterProcessManager");
InterProcessHandle.Manager = this;
this.m_InterProcess.SetCalls(InterProcessHandle.CallURL,
InterProcessHandle.IsLatestVersion,
InterProcessHandle.RequestUpdate);
return true;
}
catch (RemotingException)
{
// The server appears to be already running. Connect to
// the channel as a client and send instructions back
// to the server.
IpcClientChannel client = new IpcClientChannel();
ChannelServices.RegisterChannel(client, true);
API.InterProcess i = (API.InterProcess)Activator.GetObject(
typeof(API.InterProcess),
"ipc://" + name + "/InterProcessManager");
if (i == null)
{
Errors.Raise(Errors.ErrorType.ERROR_CAN_NOT_START_OR_CONNECT_TO_IPC);
return false;
}
if (Environment.GetCommandLineArgs().Length > 1)
i.CallURL(Environment.GetCommandLineArgs()[1]);
return false;
}
I hope this solution helps someone else :)

Categories

Resources