I am trying to use a Web service in my local. My server is written with C# and client c++ and gsoap. When I tried to compile my C++ code I am taking error below:
clientt.cpp
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) :
warning C4530: C++ exception handler used, but unwind semantics are not
enabled. Specify /EHsc
clientt.cpp(8) : error C2661: 'WebService1SoapProxy::HelloWorld' : no
overloaded function takes 1 arguments
I think, that function must take 1 argument. What's wrong with that?
Commands I used:
wsdl2h.exe -s -o calc.h calc.wsdl
soapcpp2.exe -i calc.h
cl client.cpp
.NET Web Service Server Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace webServiceDnm
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
C++ WEB Service Client Code:
/* CalcClient.cpp */
#include "soapWebService1SoapProxy.h"
#include "WebService1Soap.nsmap"
void main()
{
WebService1SoapProxy service;
char* result;
if (service.HelloWorld(result) == SOAP_OK)
std::cout<< result << std::endl;
else
service.soap_stream_fault(std::cerr);
}
Related
I create a CLR project in visual c++ with 64 bit configuration, and try to use cpprestsdk aka casablanca 64bit.
But when I run the project, an error occured:
1>------ Build started: Project: Timestamp, Configuration: Debug x64 ------
1>MyForm.cpp
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.13.26128\include\mutex(8): fatal error C1189: #error: <mutex> is not supported when compiling with /clr or /clr:pure.
1>Testapi.cpp
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.13.26128\include\mutex(8): fatal error C1189: #error: <mutex> is not supported when compiling with /clr or /clr:pure.
1>Generating Code...
1>Done building project "Timestamp.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Other error:
E2093 a local lambda is not allowed in a member function of a managed class Timestamp c:\Users\Laptop-attendance\source\repos\Timestamp\Timestamp\Testapi.h 97
The IDE shows an error about '[' characters in the .then function like .then([=](http_response response), which if you pointed it out, it says "a local lambda is not allowed in a member function of a managed class"
If I try the cpprestsdk in a Windows Console Application of Visual c++ with 64 bit configuration, it works fine.
I'm using visual studio 2017.
Do you think cpprestsdk cannot be used in CLR project of vc++?
Thanks.
Here's my code, and the code about cpprestsdk I just got it from its tutorial:
#ifndef TESTAPI_H
#define TESTAPI_H
#pragma once
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/json.h>
namespace Timestamp {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams
/// <summary>
/// Summary for Testapi
/// </summary>
public ref class Testapi : public System::Windows::Forms::Form
{
public:
Testapi(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Testapi()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->SuspendLayout();
//
// Testapi
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Name = L"Testapi";
this->Text = L"Testapi";
this->Load += gcnew System::EventHandler(this, &Testapi::Testapi_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Testapi_Load(System::Object^ sender, System::EventArgs^ e) {
auto fileStream = std::make_shared<ostream>();
// Open stream to output file.
pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
{
*fileStream = outFile;
// Create http_client to send the request.
http_client client(U("http://13.231.231.252:3000/api/individual_employment_setting/detail/172"));
// Build request URI and start the request.
//uri_builder builder(U("/search"));
//builder.append_query(U("q"), U("cpprestsdk github"));
return client.request(methods::GET);
})
// Handle response headers arriving.
.then([=](http_response response)
{
printf("Received response status code:%u\n", response.status_code());
// Write response body into the file.
// return response.body().read_to_end(fileStream->streambuf());
stringstreambuf buffer;
response.body().read_to_end(buffer).get();
//show content in console
printf("Response body: \n %s", buffer.collection().c_str());
//parse content into a JSON object:
//json::value jsonvalue = json::value::parse(buffer.collection());
return fileStream->print(buffer.collection()); //write to file anyway
})
// Close the file stream.
.then([=](size_t)
{
return fileStream->close();
});
// Wait for all the outstanding I/O to complete and handle any exceptions
try
{
requestTask.wait();
}
catch (const std::exception &e)
{
printf("Error exception:%s\n", e.what());
}
}
};
}
#endif /*TESTAPI_H*/
At the very end of this answer, Hans Passant gives a hint which is useful in your case. Basically, you need a separate c++ library (clr support turned off) where you wrap the cpprest code, link this library from your CLR project and be sure no included headers will bring <mutex> in.
Just an example, have a class like this, in a restwrapper.h header file:
class RestWrapper
{
public:
void test();
};
In its implementation file, restwrapper.cpp:
#include "restwrapper.h"
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/json.h>
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams
void RestWrapper::test()
{
auto fileStream = std::make_shared<ostream>();
// Open stream to output file.
pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
{
*fileStream = outFile;
// Create http_client to send the request.
http_client client(U("http://13.231.231.252:3000/api/individual_employment_setting/detail/172"));
//etc ...
}
You can compile this class in a DLL (linking cpprest and all its related libraries) then make your CLR project link that library. In the CLR project you need to include restwrapper.h only, which in turn includes nothing:
#include <restwrapper.h>
System::Void Testapi_Load(System::Object^ sender, System::EventArgs^ e)
{
RestWrapper wrapper;
wrapper.test();
}
You can call a class that uses mutexes from a CLR project, what you need to do is create a normal c++ project and create classes that have the required functionality but don't expose <mutex> in their headers, you are then free to call these classes from your CLR project.
Just right click your .cpp file, and under "General -> Common Language Runtime Support" select "No Common Language RunTime Support".
Simplest solution really.
I am trying to create a Windows Runtime(WinRT) Component written in C++(c++-cx) which can be called from my UWP C# app. I followed this MSDN Tutorial
It all went fine. I am able to build & run the code/sample specified in the above link on both Windows-10 Desktop & Mobile
I then tried to create a even simpler WinRT Component. I followed the same steps as above tutorial. Created a Windows Runtime Component project (File->New->Project->Visual C++ ->Windows->Universal->Windows Runtime Component(Universal Windows))
Now my class has only one function, getNum which basically returns a uint16
This is my Class header file
#pragma once
#include <collection.h>
namespace WRT_sample
{
public ref class Class1 sealed
{
public:
Class1();
uint16 getNum();
};
}
This is the corresponding cpp file:
#include "Class1.h"
using namespace WRT_sample;
using namespace Platform;
Class1::Class1()
{
}
uint16 Class1::getNum()
{
return 100;
}
I am using the same class names(Class1.h & Class1.cpp) which VS-2015 provides when I create a new WinRT project.
This is how I make a call to WinRT class from my C# code:
namespace CS_WRT_sample
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
initWRT();
}
void initWRT()
{
try
{
var nativeObj = new Class1();
var num = nativeObj.getNum();
this.Result2.Text = "Num from Cpp: " + num.ToString();
Debug.WriteLine("Num from Cpp: " + num);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
this.Result3.Text = ex.Message;
}
}
}
}
I am able to build & run my code on Desktop(x86/x64 Local Machine), but when I try to run on Mobile(x86 Emulator or ARM device), it fails with the following error:
Exception thrown: 'System.IO.FileNotFoundException' in CS_WRT_sample.exe
The specified module could not be found. (Exception from HRESULT: 0x8007007E)**
This is the stack trace which I see:
System.StubHelpers.StubHelpers.GetWinRTFactoryObject(IntPtr pCPCMD)
at WRT_sample.Class1..ctor()
at CS_WRT_sample.MainPage.initWRT()
My VS-2015 details:
MS Visual Studio Professional 2015
Version 14.0.25123.00 Update 2
MS .NET Framework
Version 4.6.01038
I am not sure, why I am hitting the issue, as I followed the same steps mentioned in the MSDN tutorial.
Any help will be highly appreciated.
You will run into this if you use a direct reference to a C++ winmd, instead of a P2P reference. This is easy to check in your .csproj:
For example, your .csproj should not have this line:
..\Debug\Abc_def\Abc_Def.winmd
Instead, consider using something like:
{1ee77824-eaa8-40f1-9b56-6f8ffa44e727}
Abc_Def
I am aware that this is a common error. I have been trying to resolve this for about a week now. I am using Visual Express 2012 and coding in C++. I start a new, but empy, win32 application. I then add a UI form to the project. In the solution there is a header file and a cpp file that I am working with. When I try and compile I get a error LNK2001 and LNK1120.
Error 1 error LNK2001: unresolved external symbol
__tWinMain C:\Users\Me\Desktop\visual c++ workspace\winFormExp01\LINK winFormExp01
Error 2 error LNK1120: 1 unresolved
externals C:\Users\Me\Desktop\visual c++
workspace\winFormExp01\Debug\winFormExp01.exe winFormExp01
The Header file:
#pragma once
namespace winFormExp01 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->SuspendLayout();
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Name = L"MyForm";
this->Text = L"MyForm";
this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
}
};
}
The cpp file:
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]
int Main(array<String^>^ args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
winFormExp01::MyForm form;
Application::Run(%form);
return 0;
}
I am trying to get a basic windows application running with as little fat as possible. All help is very much appreciated. Thanks!
you have to use WinMain instead of main.
im not sure what WinMain should look like, but if you create a new 'Windows application', or 'Windows Forms' project from the templates, hopefully one of them will have it in it and you can copy the WinMain function deceleration.
you could try this?
void __stdcall WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
long lpCmdLine,
int nCmdShow)
The starting address is a function name from the C run-time library. The linker selects it according to the attributes of the program.
mainCRTStartup (or wmainCRTStartup) An application using /SUBSYSTEM:CONSOLE;
calls main (or wmain)
WinMainCRTStartup (or wWinMainCRTStartup) An application using /SUBSYSTEM:WINDOWS;
calls WinMain (or wWinMain), which must be defined with __stdcall
So, the linker is looking for WinMain in your program which it doesn't find and complains about.
So i made a windows32 C++ dll application in visual studio 2012 and then i added a windows form in the header files section and gave it the name "UserInterface.h". When i clicked the Add button i got a popup saying "You are adding a CLR component to a native project. Your project will be converted to have Common Language Runtime support. Do you wish to continue?" and i clicked yes and it made the files "UserInterface1.cpp" and "UserInterface1.h".
but in the "UserInterface1.h" there are errors all over. here are its contents:
#pragma once
namespace AssultCubeDLL {
//ERRORS HERE: ******************************************************
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for UserInterface
/// </summary>
// ERROS HERE: *********************************************************
public ref class UserInterface : public System::Windows::Forms::Form
{
public:
UserInterface(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~UserInterface()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// Required designer variable.
/// </summary>
// ERRORS HERE: ************************************************
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->SuspendLayout();
//
// UserInterface
// ERRORS HERE: *******************************************************
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Name = L"UserInterface";
this->Text = L"UserInterface";
this->Load += gcnew System::EventHandler(this, &UserInterface::UserInterface_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void UserInterface_Load(System::Object^ sender, System::EventArgs^ e) {
}
};
}
I added comments to where the errors pop up like "Error: name followed by '::' must be a class or namespace name." does anyone know why i am getting these problems?
You will need to create a mixed mode application. Microsoft has clear instructions for the required steps.
The MS instructions will resolve the problem for System and System::Collections, but does not resolve the issues for System::ComponentModel, System::Windows::Forms, System::Data, and System::Drawing.
To compile, you must add references for the missing DLLs to the application. You can remove the using <System.Windows.Forms> from the stdafx.h file. Right-click properties and select References..., then select Add New Reference, then check the following DLLs
System
System.Data
System.Drawing
System.Windows.Forms
The code will now compile.
I put my NHibernate data access class in WCF service to can consume it by Silverlight project, but I have error and want to test my queries.
It is possible to test this queries in service class using NUnit ? Earlier I test normally this class but how do it when it is in service class ??
It's my WCF service class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using DataTransfer;
using NHibernate;
using NHibernate.Cfg;
using System.Diagnostics;
namespace WcfService1
{
public class Service1 : IService1
{
private ISession _session;
public Service1()
{
try
{
_session = (new Configuration()).Configure().BuildSessionFactory().OpenSession();
}
catch (Exception e)
{
Debug.Write(e);
throw;
}
}
public IList<Dziecko> GetChildByFirstname(string _firstname)
{
return _session.CreateCriteria(typeof(Dziecko))
.Add(NHibernate.Criterion.Expression.Eq("Imie", _firstname)).List<Dziecko>();
}
public IList<Dziecko> GetChildByLastname(string _lastname)
{
return _session.CreateCriteria(typeof(Dziecko))
.Add(NHibernate.Criterion.Expression.Eq("Nazwisko", _lastname)).List<Dziecko>();
}
public IList<Dziecko> GetChildByFirstnameAndLastname(string _firstname, string _lastname)
{
return _session.CreateCriteria(typeof(Dziecko))
.Add(NHibernate.Criterion.Expression.Eq("Imie", _firstname)).Add(NHibernate.Criterion.Expression.Eq("Nazwisko", _lastname)).List<Dziecko>();
}
}
}
If you want to test the queries themselves, I'd recommend putting them into a separate assembly (maybe using the repository pattern) and calling the methods in this assembly from your service. This will make it easier to test the queries themselves, and also allow you to mock the repositories when testing the service.