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.
Related
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.
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.
I have an application developed in VB 6.0. I don't have access to its code. This application also exposes its functionality through certain API provided in its dlls. Is there a way for me to check what methods of the API the consumers of this application's API are calling across anywhere the API is deployed. I want a C# program to just sit in that target environment and intercept the calls made to that API and report it back to my service via a service. I wont be modifying the API or the code calling the API. Is this possible in C# or would I need to go with C++?
Update
Lets say for sake of simplicity, that its a simple VB application developed in VB 6 called SimpleAPP, and it has a button that displays records in a grid. It does this by calling a component CMPA.dll with a public method GetRecords(string ID) which returns an Array of records. I have another few applications called CustomerApp.exe and AnotherCustomerApp.exe which also have a reference to CMPA.dll and they both calls this same method to get the records. Now, I want to develop a program called Interceptor.exe that will actually sit in the environment where CustomerApp and AnotherCustomerApp is deployed and will log internally which of these two applications called that CMPA dll's public method GetRecords and also log what parameter it sent in and what results were retrieved.
I had to google to find the library that was on the tip of my tongue.
That googling turned up some interesting articles: a new to me 1999 Microsoft Research article called “Intercepting and Instrumenting COM Applications” and an Microsoft Systems Journal article from january 1999 that I do remember, “Building a Lightweight COM Interception Framework”.
The library you want is probably Microsoft Detours. I have only used it from C++, not from C#, and I have only used it for intercepting calls to Windows API functions, not COM methods, so I can’t guarantee that it’s well suited. But it's not exactly rocket science to interface these two languages, if needed.
If Detours doesn’t turn out to fill your needs, then look at the articles cited. Quite possibly they resulted in some framework you can use. And otherwise they have the information you need to build your own. You might then also check out if ParkPlace ever made what you want. There was once great interest in “cross concern“ functionality, and ParcPlace did some of the most interesting research, as I recall.
I am new to .Net and hitting the brick wall trying to resolve this....
Having done enough googling for the past few days I've come across nothing but some vague (at lease for me) C# related info
Basically, I am trying to set up a few global hooks to carry out certain automation process. Since the development environment is VS2008 C++ windows forms, I started by compiling a native Dll to be injected by the calling prog. The strategy being for the callback proc in native dll calling a function in .Net program (or maybe a wrapper managed dll), passing the filtered raw data (keyboard/mouse/WM_create/etc) messages for further processing.
Question: How do I pass on the handle of such function(s) to my injected dll?
Is the managed wrapper dll path an easier choice or simply have the managed & native functions residing side by side in the main application?
I'll have to do a lot of Marshalling which is yet another dark side of the matter. Is there a link to precise documentation/examples of marshalling functions?
I thank you for your help in advance.
Mark
Have a look at 'Marshal.GetFunctionPointerForDelegate'
I have a (kind of) database implemented in C++.
Now I want to create a Windows service for querying it, because P-invoking it is not an option, as the database would have to be loaded with every query, which takes several minutes.
But I face several problems:
How can I create a C++ Windows Service in VS2010? The template has been removed (why???), can I use a 2008 template and convert it? If yes: where do I find such a template?
Supposed I manage to create a C++ Windows service: what are my options to communicate with this sevice (from c sharp)? What are the advantages/disadvantages?
I'd be glad for any hint!
Ben
I don't know why, but the template is really gone.
For communicating between your application and the service WCF would be an option. One advantage of WCF is, that you can easily switch the transport layer (HTTP, TCP, Shared Memory)
Have a look at this MSDN page. It describes exactly what you are trying to achieve: Host a WCF Service in a Windows Service. It even contains a simple Windows Service implementation at the end. Unfortunately no C++ but C#.
you might want to take a look at POCO Project,in particular this class. There are examples if you download the code.
you might also want to implement a web service for easy access from any client anywhere through sockets.
What I did some years ago was an COM out-of-process server which was a Windows Service. It worked fine and you can access it from C# (and many other languages ...) easily. If you have no COM experience it might become hard (depending on how complex your interface is).
You should implement the followin functions:
VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv);
VOID WINAPI ServiceHandler(DWORD fdwControl);
VOID ReportSvcStatus( DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint );
More info on MSDN. An example: link.
Maybe you can use ! ATL Project and then select Service (EXE).
Best Regards