libcurl compile errors - c++-cli

I use libcurl and I get these errors without writting any code just compiling and I don't know why
Fehler 49 error C2628: '$UnnamedClass$0x05e5b255$395$' gefolgt von 'bool' unzulässig (Semikolon ';' vergessen?) c:\users\ttg\desktop\curl-7.21.7\lib\setup_once.h 273
Fehler 50 error C2065: '_SH_DENYNO': nichtdeklarierter Bezeichner C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xiosbase 111
first is a libcurl file and it says it's followed by bool(forgot semicolon?)
second undeclared identifier
this is the whole code
#pragma once
#include <curl/curl.h>
#include <curl/easy.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
namespace fr {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
// assert(bodyfile == (FILE*) stream); //this assertion fails, but when i comment it, code works. Why?
int written = fwrite(ptr, size, nmemb, (FILE *)stream);
return written;
}
/// <summary>
/// Zusammenfassung für Form1
///
/// Warnung: Wenn Sie den Namen dieser Klasse ändern, müssen Sie auch
/// die Ressourcendateiname-Eigenschaft für das Tool zur Kompilierung verwalteter Ressourcen ändern,
/// das allen RESX-Dateien zugewiesen ist, von denen diese Klasse abhängt.
/// Anderenfalls können die Designer nicht korrekt mit den lokalisierten Ressourcen
/// arbeiten, die diesem Formular zugewiesen sind.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Konstruktorcode hier hinzufügen.
//
}
protected:
/// <summary>
/// Verwendete Ressourcen bereinigen.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
protected:
protected:
private:
/// <summary>
/// Erforderliche Designervariable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
void InitializeComponent(void)
{
this->SuspendLayout();
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void backgroundWorker1_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {
CURL *curl_handle;
const char *headerfilename = "head.out";
FILE *headerfile;
const char *bodyfilename = "body.html";
FILE *bodyfile;
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://url");
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
headerfile = fopen(headerfilename,"w");
if (headerfile == NULL) {
curl_easy_cleanup(curl_handle);
return;
}
bodyfile = fopen(bodyfilename,"w");
if (bodyfile == NULL) {
curl_easy_cleanup(curl_handle);
return;
}
curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER, headerfile);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);
curl_easy_perform(curl_handle);
fclose(headerfile);
fclose(bodyfile);
curl_easy_cleanup(curl_handle);
}
};
}
that's the explanation for second problem:
1>C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xiosbase(111) : error C2065: '_SH_DENYNO': nichtdeklarierter Bezeichner
1> C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xiosbase(196): Siehe Verweis auf die Instanziierung der gerade kompilierten Klassen-template "std::_Iosb<_Dummy>".
1> with
1> [
1> _Dummy=int
1> ]
translation
error C2065: '_SH_DENYNO': undeclared identifier
See reference to the instantiation of the classes-template being compiled

Because of there's a file named share.h in libCURL.
You should rename it into curl_share.h
& replace all
#include "share.h"
by
#include "curl_share.h"

Adjust you your include path only to c:\users\ttg\desktop\curl-7.21.7\include and not to include c:\users\ttg\desktop\curl-7.21.7\lib also.
in the lib directory there is a share.h in root the conflicts with VC\include\share.h.

Taking a wild guess, I would say you are compiling C, but including C++ headers. <string> <sstream> and <iostream> are C++ headers.

You can fix the error about bool by defining HAVE_BOOL_T in your preprocessor and TWO files named share.h were referenced in your project, one in libcurl and another in your standard template library, that's why you get such errors about _SH_DENYNO.

Related

class factory error when test a COM server

I am implementing a COM component
import "unknwn.idl";
[
uuid(CF86E2E0-B12D-4C6A-9C5A-D7AA65101E91),
]
coclass CClassic
{
[default] interface IClassic;
}
[
object,
uuid(CF86E2E0-B12D-4C6A-9C5A-D7AA65101E90),
pointer_default(unique),
oleautomation
]
interface IClassic : IUnknown
{
HRESULT Hello();
}
and encapsulated it in an in-process dll and an out-of-process exe respectively.
In the dll case , I can call the interface method successfully.
But in the exe case, got the 80040154 error. Following this doc, I registered the proxy and checked the registry, all the needed registry keys/values are there.
How does this happen? I doubt something is missing in the above idl file. Many thanks!!!
Update
comexe.cpp
#include <atlbase.h>
#include <atlcom.h>
/*
cl /nologo /EHsc comexe.cpp comobj.cpp classic/classic_i.c /link /debug
cl /nologo /EHsc /DREGISTER_PROXY_DLL classic/classic_p.c classic/classic_i.c classic/dlldata.c /link /dll /out:comproxy.dll /export:DllGetClassObject /export:DllCanUnloadNow /export:DllRegisterServer /export:DllUnregisterServer /export:GetProxyDllInfo rpcrt4.lib
*/
struct MyModule : CAtlExeModuleT<MyModule>
{
HRESULT RegisterClassObjects(
_In_ DWORD dwClsContext,
_In_ DWORD dwFlags)
{
auto hr = CAtlExeModuleT::RegisterClassObjects(dwClsContext,dwFlags);
printf("RegisterClassObjects 0x%x\n",hr);
return hr;
}
HRESULT RegisterServer(
_In_ BOOL bRegTypeLib = FALSE,
_In_opt_ const CLSID* pCLSID = NULL)
{
auto hr = CAtlExeModuleT::RegisterServer(false, pCLSID);
if(FAILED(hr))
printf("Toaster register failed 0x%x\n",hr);
return hr;
}
};
MyModule mod;
int main(int argc, char *args[])
{
mod.WinMain(SW_HIDE);
}
comobj.cpp
#include <atlbase.h>
#include <atlcom.h>
#include <typeinfo>
//#include "comobj.h"
#include "classic/classic.h"
CComModule _Module;
extern "C" CLSID CLSID_CClassic;
struct Toast : CComObjectRoot,CComCoClass<Toast,&CLSID_CClassic>,IClassic
{
DECLARE_REGISTRY(CLSID_CClassic,"Toaster.1.0","Toaster","Toast Server",THREADFLAGS_BOTH);
DECLARE_CLASSFACTORY();
DECLARE_OBJECT_DESCRIPTION("Toast Server");
HRESULT STDMETHODCALLTYPE Hello( void)
{
printf("Hello\n");
return S_OK;
}
Toast()
{
printf("Toast alive\n");
printf("%s\n",typeid(_ClassFactoryCreatorClass).name());
}
BEGIN_COM_MAP(Toast)
COM_INTERFACE_ENTRY(IClassic)
END_COM_MAP();
};
OBJECT_ENTRY_AUTO(CLSID_CClassic,Toast);

Windows Form App With Inline Assembly Code

I am very confused and frustrated, I am in an assembly programming class and am tasked with modifying an example from the textbook for one on my assignment problems. The example is a simple string encrypter which uses inline assembly to modify characters from an input string one-by-one and print it back out in another textbox within a windows form.
I wrote everything exactly as it is in the text and no dice Visual Studio will not build it and tons of build errors are thrown. I don't understand what I am doing wrong, and it is frustrating when I cannot practically apply something that is demonstrated in the text.
Here is the example code:
Windows Form Data Encryption
And here is what I have which is in the header file .h that is part of the form design:
#pragma once
char EncryptionKey = 0x45;
char Encrypt(char code)
{
_asm
{
mov a1, code
xor a1, EncryptionKey
mov code, a1
mov a1, EncryptionKey
inc a1
and a1, 7fh
mov EncryptionKey, a1
}
return code;
}
namespace TextEncryter {
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 Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ Encrypt;
protected:
private: System::Windows::Forms::RichTextBox^ richTextBox1;
private: System::Windows::Forms::RichTextBox^ richTextBox2;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
protected:
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->Encrypt = (gcnew System::Windows::Forms::Button());
this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
this->richTextBox2 = (gcnew System::Windows::Forms::RichTextBox());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// Encrypt
//
this->Encrypt->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->Encrypt->Location = System::Drawing::Point(197, 249);
this->Encrypt->Name = L"Encrypt";
this->Encrypt->Size = System::Drawing::Size(75, 23);
this->Encrypt->TabIndex = 0;
this->Encrypt->Text = L"Encrypt";
this->Encrypt->UseVisualStyleBackColor = true;
this->Encrypt->Click += gcnew System::EventHandler(this, &Form1::Encrypt_Click);
//
// richTextBox1
//
this->richTextBox1->Location = System::Drawing::Point(13, 22);
this->richTextBox1->Name = L"richTextBox1";
this->richTextBox1->Size = System::Drawing::Size(259, 96);
this->richTextBox1->TabIndex = 1;
this->richTextBox1->Text = L"";
//
// richTextBox2
//
this->richTextBox2->Location = System::Drawing::Point(13, 138);
this->richTextBox2->Name = L"richTextBox2";
this->richTextBox2->Size = System::Drawing::Size(259, 96);
this->richTextBox2->TabIndex = 2;
this->richTextBox2->Text = L"";
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label1->Location = System::Drawing::Point(13, 3);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(36, 16);
this->label1->TabIndex = 3;
this->label1->Text = L"Input";
this->label1->Click += gcnew System::EventHandler(this, &Form1::label1_Click);
//
// label2
//
this->label2->AutoSize = true;
this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label2->Location = System::Drawing::Point(10, 121);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(98, 16);
this->label2->TabIndex = 4;
this->label2->Text = L"Encrypted Text";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 284);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->richTextBox2);
this->Controls->Add(this->richTextBox1);
this->Controls->Add(this->Encrypt);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
*private: System::Void Encrypt_Click(System::Object^ sender, System::EventArgs^ e)
{
richTextBox1->Text = "";
for (int a = 0; a < textBox1->Text->Length; a++)
{
richTextBox1->Text += Convert::ToChar(Encrypt(textBox1->Text[a]));
}
}*
};
}
I am using Visual Studio 2008 Express. When I try and use a newer version of visual studio windows forms are based on C# and not C++ and I can't even get it to build as far as in 2008. The main errors that are thrown are:
1> Inline native assembly not supported in managed code
error C3862: 'Encrypt': cannot compile an unmanaged function with /clr:pure or /clr:safe
C2227: left of '->Text' must point to class/struct/union/generic type
1> type is ''unknown-type''
Do I maybe need to put the char Encrypt function within the .ccp source file that belongs to the project? Any help would be greatly appreciated.

Can't compile thread example in Visual C++ 2005 using "oldSyntax" switch

I am trying to follow this example on how to create threads in a windows form. Purely following the example I get multiple syntax errors upon building.
I am using the /clr:oldSyntax compiler switch to compile the example.
Initialization of the Form1 class is the first source of the error:
public ref class Form1 : public System::Windows::Forms::Form
{
Errors:
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(24) : error C2059: syntax error : 'public'
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(24) : error C2059: syntax error : 'public'
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(25) : error C2143: syntax error : missing ';' before '{'
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(25) : error C2447: '{' : missing function header (old-style formal list?)
Where exactly are these errors coming from?
Full Code:
#pragma once
using namespace System::Threading;
namespace ThreadExample
{
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 Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::ProgressBar^ progressBar1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
private: Thread *trd;
#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->button1 = (gcnew System::Windows::Forms::Button());
this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(197, 12);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// progressBar1
//
this->progressBar1->Location = System::Drawing::Point(94, 162);
this->progressBar1->Name = L"progressBar1";
this->progressBar1->Size = System::Drawing::Size(100, 23);
this->progressBar1->TabIndex = 1;
this->progressBar1->Click += gcnew System::EventHandler(this, &Form1::progressBar1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Controls->Add(this->progressBar1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
ThreadStart *myThreadDelegate = new ThreadStart(this, repeat);
trd = new Thread(myThreadDelegate);
trd->IsBackground = true;
trd->Start();
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
MessageBox::Show(S"This is the main thread");
}
private: System::Void progressBar1_Click(System::Object^ sender, System::EventArgs^ e)
{
}
};
__delegate void DelegateThreadTask();
private: void ThreadTask()
{
int stp;
int newval;
Random *rnd=new Random();
if (progressBar1->InvokeRequired == false)
{
stp=this->progressBar1->Step*rnd->Next(-1,2);
newval = this->progressBar1->Value + stp;
if (newval > this->progressBar1->Maximum)
newval = this->progressBar1->Maximum;
else if (newval < this->progressBar1->Minimum)
newval = this->progressBar1->Minimum;
this->progressBar1->Value = newval;
}
else
{
DelegateThreadTask *myThreadDelegate = new DelegateThreadTask(this,ThreadTask);
this->Invoke(myThreadDelegate);
}
}
private: void repeat()
{
while(true)
{
ThreadTask();
Thread::Sleep(100);
}
}
}
The old syntax for C++/CLI uses __gc to declare classes.
References:
C++/CLI language specification from Visual Studio 2003, when the 'old' syntax was the only syntax.
VS2005 documentation giving both old & new syntax for declaring managed types in C++/CLI.
Test 1:
public ref class Foo
{
};
Compile result:
OldSyntax.cpp
OldSyntax.cpp(1): error C2059: syntax error : 'public'
OldSyntax.cpp(2): error C2143: syntax error : missing ';' before '{'
OldSyntax.cpp(2): error C2447: '{' : missing function header (old-style formal list?)
Test 2:
public __gc class Foo
{
};
Compile result: no errors.
(Note: I used VS2012 for my test. I don't have VS2005 installed anymore; the error messages may be slightly different on 2005.)

Importing an Embarcadero C++ Builder XE3 DLL into Embarcadero C++ Builder XE3

I try to create a DLL in Embarcadero C++ Builder XE3, and use it in a test-project in the same environment.
I take example on a tutorial which code does not give a good result for me (!) : http://docwiki.embarcadero.com/RADStudio/XE3/en/Tutorial:_Using_Dynamic_Linked_Libraries_in_C%2B%2BBuilder_Applications
Here is the content of my DLL :
BaseAuth.h file :
#ifndef BaseAuthH
#define BaseAuthH
#include <System.hpp>
class TBaseAuth
{
public:
virtual void TestMessage() = 0;
};
#endif // BaseAuthH
Auth.h file :
//---------------------------------------------------------------------------
#ifndef AuthH
#define AuthH
//---------------------------------------------------------------------------
#include "BaseAuth.h"
class TAuth : public TBaseAuth
{
public:
TAuth();
~TAuth();
void TestMessage();
};
#endif
Auth.cpp file :
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Auth.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
TAuth::TAuth()
{
}
TAuth::~TAuth()
{
}
void TAuth::TestMessage()
{
MessageBox(0, "Test message", "Test", MB_OK);
}
and File1.cpp :
#pragma hdrstop
#pragma argsused
#include "Auth.h"
extern "C" __declspec(dllexport) void* __stdcall GetClassInstance()
{
return static_cast<void*>(new TAuth());
}
extern "C" int _libmain(unsigned long reason)
{
return 1;
}
Now in the test application I have :
the same BaseAuth.h file
a form with a Button :
Test_DLLAuthOrga.h :
#ifndef Test_DLLAuthOrgaH
#define Test_DLLAuthOrgaH
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
#include "BaseAuth.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // Composants gérés par l'EDI
TButton *Button2;
void __fastcall Button2Click(TObject *Sender);
private: // Déclarations utilisateur
TBaseAuth *mpAuth;
public: // Déclarations utilisateur
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Test_DLLAuthOrga.cpp :
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Test_DLLAuthOrga.h"
#include "BaseAuth.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
const wchar_t* library = L"DLLAuthOrga.dll";
extern "C" __declspec(dllimport) void* __stdcall GetClassInstance();
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
HINSTANCE load;
try
{ load = LoadLibrary(library); }
catch(Exception &e)
{ ShowMessage(e.Message); }
if (load)
{
ShowMessage("Library Loaded!");
void *myFunc;
myFunc = (void *)GetProcAddress(load, "GetClassInstance");
mpAuth = (AuthParent*)myFunc;
}
else { ShowMessage("Library not loaded!"); }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (mpAuth == NULL) return;
try { pRes = mpAuth->TestMessage(); }
catch(Exception &e) { ShowMessage(e.Message); return; }
}
The result is :
the pointer mpAuth has an adress.
But its methods have no adress, and when I call a simple method such as "void TestMessage()", it raises an access violation.
=> It first seemed to be a question of string compatibility (but between "C++ Builder XE3" and "C++ Builder XE3" I would expect the same string format to be used ?!) : Error calling DLL with Unicode Delphi
=> I found a similar issue but with C++ DLL into Delphi, not C++ DLL into C++ ... : Call C++ DLL in Delphi app
=> I tried using "HMODULE" instead of "HINSTANCE load;" : same result.
=> I tried without success using
mpAuth = static_cast<AuthParent*>(myFunc);
instead of :
mpAuth = (AuthParent*)myFunc;
=> I also tried replacing "__stdcall" by "__cdecl" or "" (removing) : the libray loads but GetProcAdress returns NULL.
=> What am I doing wrong in attempting to call the DLL's method "TestMessage()" ?
To correctly bind function from dll you should give it full definition, including calling convention, arguments, return type and __dllexport/__dllimport modifier. The easiest way to do it - using typedef so instead of typing (in Test_DLLAuthOrga.cpp)
void *myFunc;
myFunc = (void *)GetProcAddress(load, "GetClassInstance");
use
typedef __declspec(dllimport) void (__stdcall *MyFuncPointerType)(void);
MyFuncPointerType myFunc;
myFunc = (MyFuncPointerType)GetProcAddress(load, "GetClassInstance");
If you are using __cdecl convention you should also add underscore to the target function name
typedef __declspec(dllimport) void (__cdecl *MyFuncPointerType)(void);
MyFuncPointerType myFunc;
myFunc = (MyFuncPointerType)GetProcAddress(load, "_GetClassInstance");
you can also explicitly define AuthParent* as a return type for your factory function to get rid of uneccessary casts to void* and back to AuthParent* (in File1.cpp and Test_DLLAuthOrga.cpp) so, the final code snippet would look like this:
typedef __declspec(dllimport) AuthParent* (__cdecl *MyFuncPointerType)(void);
MyFuncPointerType myFunc;
myFunc = (MyFuncPointerType)GetProcAddress(load, "_GetClassInstance");
mpAuth = myFunc();

Mono CSharp Evaluator : creating two Action<object> via two distinct Run() crashes

I'm trying to embed mono in a c++ executable, and mono crashes on the second evaluator.Run(..) as below. Any idea of what I missed ?
Using mono 3.0.3.
EmbeddedMonoTest.cpp
// EmbeddedMonoTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/exception.h>
#include <mono/jit/jit.h>
#include <mono/metadata/assembly.h>
int _tmain(int argc, _TCHAR* argv[])
{
MonoDomain* domain = mono_jit_init_version ("ClassLibrary1", "v4.0.30319");
MonoAssembly* assembly = mono_domain_assembly_open (domain, "ClassLibrary1.dll");
mono_assembly_get_image(mono_domain_assembly_open (domain, "Mono.CSharp.dll"));
MonoImage* image = mono_assembly_get_image (assembly);
MonoClass* klass = mono_class_from_name(image, "ClassLibrary1", "Class1");
MonoMethod* test = mono_class_get_method_from_name(klass, "Test", 0);
mono_runtime_invoke(test, NULL, NULL, NULL);
return 0;
}
Class1.cs
using System;
using System.Reflection;
using Mono.CSharp;
namespace ClassLibrary1
{
public class Class1
{
public static void Test()
{
var assembly = Assembly.GetAssembly(typeof(Class1));
CompilerSettings settings = new CompilerSettings();
ReportPrinter printer = new ConsoleReportPrinter();
CompilerContext context = new CompilerContext(settings, printer);
Evaluator evaluator = new Evaluator(context);
evaluator.ReferenceAssembly(assembly);
evaluator.Run("using System; using ClassLibrary1;");
evaluator.Run("Action<object> action = args => {{ 'x'.ToString(); }}; ");
evaluator.Run("Action<object> b = args => {{ 'x'.ToString(); }}; ");
}
}
}
The error :
Unhandled exception at 0x0274b00d in EmbeddedMonoTest.exe: 0xC0000005: Access violation reading location 0x00000000.