So, I've written a web service in c#, which has a method for signing a hash. This web service is a WCF Service application.
Then, I've created a c# Console application where I've written a function to consume this web service.
The declaration of the function which calls the web service is that:
class Program
{
public byte[] callWS(string alias, byte[] myHash,string myPassword)
{
IhashSignSVCClient client = new IhashSignSVCClient();
byte[] signedData= client.SignandReturn(alias, myhash, myPassword);
if (signedData != null)
{
Console.WriteLine(signedData);
return signedData;
}
return null;
}
}
This web service works fine. Now I want to build a c++ wrapper class over this c# method, because I want to call this web service from unmanaged code and I thought that creating a c++ wrapper class will be a good idea. Can anyone help me with a kind of a structure of this wrapper class? I haven't understood very well the conversions between c++ and c#. I've created a C++ CLR class library, with a ref class which will contain my c++ method to call this c# method, but I still have some problems with the type of parameters of this function.
If you want to stick to native (unmanaged) C++ (instead of using C++/CLI), your best option might be WWSAPI as that can call a WCF service directly from C++. Although since it's a C-based API, it's a bit of a nuisance to use from C++.
Otherwise, there is extensive documentation about C++/CLI here; in particular marshaling.
Related
This is a design suggestion I would like to have from the C++ Web Service experts here.
I'm working on a C++ Windows DLL that currently is used by a exe. The plan is to create a Web Service and expose the functions in the DLL to the web.
From what I researched so far, I came across some libraries that will enable a C++ application to access REST APIs. Buy in my case, Am looking to create a Web Service that will expose the functions in the C++ DLL.
Am looking for something that doesn't involve too much learning curve and can be implemented easily.
Some of the C++ functions currently take vectors as inputs and return a huge vector.
So the capability to input and output large amounts of data is needed. i.e., the exposed Web Service functions should be able to have vectors as arguments passed to it.
Hope to get some valuable suggestions.
Would recommend WCF web service if it's a windows DLL. You can always call C++ functions from C# code.
A simple WCF API will look something like this
[OperationContract]
[WebGet(UriTemplate = "/Test/{name}")]
string Test(string name);
In the *.svc.cs file the implementation goes
public string Test(string name)
{
return "Hello " + name;
}
And to call C++ functions use the dllimport.
Let me know if you need any further help.
We have a MFC application setup where a client receives data from server whenever new data is available. Client is being rewritten in C# but communication with server part is in MFC. We have written a C++/CLI wrapper to classes associated with MFC and are able to receive data on application initialization. Communication engine is still in MFC and we have created wrapper classes only for data items. When we initialize the CLI wrapper we copy the data from MFC arrays to CLI arrays. But am not sure how to get the data which is received lets say after 5 seconds. How can it notify C++/CLI wrapper that new data has been received by MFC code running in the background. Please let me know if you need clarifications. Thanks.
or
Is there a way to invoke a C++/CLI method from C++ code ?
NOTE:
its a mixed mode compilation which has vc++ and C++/CLI code.
Edit ::
As mentioned by Hans below added request for callback solutions.
Resolved this issue by converting, CPP events to CLI delegates, which are recognized by .NET as .NET delegates.
this way we can control actions at C# end from CPP. If anybody has a better solution please share.
I have been trying to create a UWP class library that gives me access to Windows 10s native features such as Windows.Security.Authentication.OnlineId. I would like to get a username and ID from the device for use in a Unity UWP IL2CPP project. I am currently able to do this with Unity's built in social class for ios and there is code which google has written that allows this to work seamlessly with the same class but for android's Google Play Games.
I've downloaded a sample off github (https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/WebAccountManagement) which demonstrates how to call the relevant classes and functions in a UWP app and works well but the samples appear to be are accessing classes from the "Windows.Foundation.UniversalApiContract" class.
I can't seem to find a way to add this to a basic UWP class library so I can call on the required classes such as Windows::Security::Credentials::WebAccountProvider.
The best I've been able to do is create a basic function in the class library that returns a small hardcoded string just to test if the concept was remotely possible.
:-
extern "C" __declspec(dllexport) wchar_t* __stdcall GetMyString()
{
wchar_t* myString = L"Guuuper";
auto resultBufferLength = wcslen(myString) + 1;
wchar_t* result = static_cast<wchar_t*>(CoTaskMemAlloc(resultBufferLength * sizeof(wchar_t)));
wcscpy_s(result, resultBufferLength, myString);
return result;
}
My whole journey in attempting to do that can be found here:-
http://forum.unity3d.com/threads/returning-c-string-to-il2cpp-windows-store-project.395284/
I've been able to successfully call this code from within unity via a UWP build but my main question is how I would go about adding the appropriate references or how I would create this class library to access the WebAccountProvider class?
Any help would be much appreciated
Update: I have asked the MS team at their own site about this challenge and they appear to be working on a solution.
Apologies for 'answering' and not 'commenting' but I'm still a new contributor.
My initial first instinct is to suspect that your problem isn't with Unity3D; it's with C# and the entire programming environment inside Unity3D. C# does not allow for unmanaged memory allocation. There's an entire art of getting C++ libraries to work in C#, and it's called "Marshaling" code, and there's an entire industry based around marshaling plugins from C++ to Unity3D as a result.
The reason you're not getting a string, is because you're literally sending and receiving a pointer to a single character.
Unfortunately, my single experience marshaling C++ code for Unity was five years ago and I'm a little rusty on what my solutions were. What I do remember is that the most 'hacky' but obvious solution was to work out the maximum size of string that could possibly be passed, and, on both sides of the divide, pass and receive strings of that predefined size.
https://learn.microsoft.com/en-us/dotnet/framework/interop/default-marshaling-for-strings
Let us know if that sends you in the right direction.
This is the Documentation from Oracle Docs. I want to clarify certain jargon based questions.
On the server side, the developer specifies the web service operations by defining methods in an interface written in the Java programming language. The developer also codes one or more classes that implement those methods. Client programs are also easy to code. A client creates a proxy (a local object representing the service) and then simply invokes methods on the proxy.
In The first Bold lettered sentence,
Are these classes, the Implementation classes of the Web service ?
Second Bold lettered,
will the client create the object of those service implementation classes? If yes, how come? Will JAX WS transport the complete service implementation class code which is # server to the client?
I am very new to the concept of web services. if my doubt is silly please bear with me. Thanks!
First point. Yes you code the implementation of the web service. However this is just limited to the business logic you wish to execute you don't have to go code the low level boiler plate code like creating a HTTP socket etc.
A simple class that is exposed as a web service will look like this:
#Webservice()
public class CalculatorWS()
{
#WebMethod(#operationame="add")
public int add(#WebParam(name="i") int i, WebParam(name="j") int j)
{
//this is where you code your implementation
return i+ j;
}
}
A client proxy class does NOT transfer the implementation across the wire. It just creates a proxy that you can use to call the implementation.
You can learn all about it step by step by following this tutorial. It is easy one to understand and follow and will answer all of your questions.
Scenario:
I am trying to migrate a C++ application to WinRT/Metro Style. This application uses an ATL/COM object that implements an IDispatch interface by using the class IDispatchImpl, however, according to MSDN IDispatchImpl is not available for Metro Style applications.
My ATL/COM class looks like this:
class MyATLClass :
public IDispatchImpl<IMyDispInterface, &IID_IMyDispInterface, &LIBID_MYLIB, 1, 0>,
public CComObjectRoot,
public CComCoClass<MyATLClass,&CLSID_MyATLClass>
{
...
}
Question:
Is there any replacement in WinRT for IDispatchImpl?
The replacement could involve deriving from different classes and discarding my IDL file for example. My ultimate goal is just to be able to do QueryInterface on an instance of MyATLClass and get a reference through IMyDispInterface. I can also include all my files (library and application) in a single project, but I do want to avoid changing the code where IMyDispInterface references are used if possible.
Re-implementing my COM/ATL class as a WRL based component is probably the best choice in this scenario (Thanks Larry). More information is provided on these video posts:
Porting a desktop app to a Metro style app
The Windows Runtime Library (WRL)