C++ : How to extern a class in c++ / cli windows form apps? - class

Sorry my bad English first of all.
I just want to use a class globally in my project but i receive some error.
I researched and tried similar titles but didnt work. Maybe i could not do.
code.h
#ifndef CODE_H_
#define CODE_H_
class sinif
{
public: void doSomeThing()
{
/*
...
*/
}
private:
};
sinif A;
#endif
Form1.h
//that is main form
#include "code.h"
#include "b.h"
namespace project
{
/*
...
A.doSomeThing(); // it works
*/
}
b.h
//that is second mini form
#include "code.h"
extern sinif A;
namespace project
{
/*
...
A.doSomeThing(); //it didn't work
*/
}

Related

ESP8266 Scheduler. Tasks in separate files arduino IDE

I want to create simple weather station with esp8266 but with scheduler to updating data and GUI more simultaneously. I've downloaded scheduler from here but there is information:
Tasks must be declared globally on the stack (not a pointer). Failure to do so will crash your device
Does that mean that I have to write all task classes in *.ino file? Can I save them in separate files and call to sketch file? How to do that? I've tried few times but the code won't compile.
Simpliest example of sketch:
#include <Scheduler.h>
#include <Arduino.h>
class SimpleTask : public Task {
protected:
void setup() {
Serial.println("Setup func");
}
void loop() {
Serial.println("Loop func");
delay(600);
}
} simple_task;
void setup() {
Serial.begin(115200);
Scheduler.start(&simple_task);
Scheduler.begin();
}
void loop() { }
Tasks must be declared globally on the stack (not a pointer).
I think strictly this means that Tasks must not be created on the heap and need to be global, static objects.
This can be done by creating the object in a separate file and declaring the object using the extern keyword.
e.g. an example mysimpletask.h:
#ifndef __MySIMPLETASK_H__
#define __MySIMPLETASK_H__
#include <Scheduler.h>
class MySimpleTask : public Task {
protected:
void setup();
void loop();
};
extern MySimpleTask my_simple_task;
#endif
The implementation in mysimpletask.cpp:
#include "mysimpletask.h"
void MySimpleTask::setup() {
Serial.println("Setup func");
}
void MySimpleTask::loop() {
Serial.println("Loop func");
delay(600);
}
MySimpleTask my_simple_task;
And the sketch:
#include <Scheduler.h>
#include <Arduino.h>
#include "mysimpletask.h"
void setup() {
Serial.begin(115200);
Scheduler.start(&my_simple_task);
Scheduler.begin();
}
void loop() { }

Unmanaged method member being used as a delegate in managed class

I have a project where I am expanding existing native C++ compatible code functionality for a customer-facing library by integrating a driver from a managed DLL, which seems like the opposite direction for most marshaling questions. Since there can be multiple devices using this one driver, each device needs to be its own object, with its own callback methods. This integration will take the form of a native wrapper DLL that exposes the native class interface to the customer-facing library, which will pass an instance of a member method callback function pointer to the DLL. I need to pass a native method member instance (non-static) as a delegate to the managed wrapper class. The managed wrapper class uses GetDelegateForFunctionPointer, which works to marshal static native pointers to a delegate, but I can't find any information if it works with instances. GetDelegateForFunctionPointer also takes a System::IntPtr argument for the function pointer, but I'm also not sure if System::IntPtr works for instances, and when compiling in VC++2008 I'm being presented an error C3867 for attempting this.
Managed wrapper header:
//somemanagedclass.hpp
#pragma once
using namespace System;
using namespace ExternalManagedLibrary;
namespace SomeNameSpace
{
public ref class SomeManagedClass
{
public:
SomeManagedClass();
~SomeManagedClass();
delegate void CallbackHandler(const wchar_t*, int);
CallbackHandler^ CallbackEvent;
void RegisterCallback(IntPtr callbackEvent);
private:
ExternalManagedClass^ externalManagedClass;
void OnCallback(Object^ sender, ValueEventArgs<String^>^ e);
};
}
Managed wrapper source:
//somemanagedclass.cpp
#include "somemanagedclass.hpp"
#include <vcclr.h>
using namespace System;
using namespace Runtime::InteropServices;
namespace SomeNameSpace
{
SomeManagedClass::SomeManagedClass()
{
externalManagedClass = gcnew ExternalManagedClass();
externalManagedClass->CallbackEvent += gcnew EventHandler<ValueEventArgs<String^>^>(this, &SomeManagedClass::OnCallback);
}
SomeManagedClass::~SomeManagedClass()
{
externalManagedClass->CallbackEvent -= gcnew EventHandler<ValueEventArgs<String^>^>(this, &SomeManagedClass::OnCallback);
}
void SomeManagedClass::OnCallback(Object^ sender, ValueEventArgs<String^>^ e)
{
String^ some_string = String::Copy(e->Value);
cli::pin_ptr<const wchar_t> pinned_string = &PtrToStringChars(some_string)[0];
const wchar_t* p = pinned_string;
CallbackEvent(pinned_string, some_string->Length);
}
void SomeManagedClass::RegisterCallback(IntPtr callbackEvent)
{
CallbackEvent = (CallbackHandler^)(Marshal::GetDelegateForFunctionPointer(callbackEvent, CallbackHandler::typeid));
}
}
Native wrapper interface:
//somenativeinterface.hpp
#ifdef DLL_EXPORT
#define IMPORT_EXPORT __declspec(dllexport)
#else
#define IMPORT_EXPORT __declspec(dllimport)
#endif //DLL_EXPORT
typedef void (*NativeCallback)(const unsigned char*, unsigned long);
class IMPORT_EXPORT SomeNativeInterface
{
public:
//class factory
static SomeNativeInterface* Create(void);
static void Destroy(SomeNativeInterface* clInterface);
virtual void CallbackInit(NativeCallback fnNativeCallbackInit);
};
Native wrapper header:
//somenativeclass.hpp
#pragma once
#include "somenativeinterface.hpp"
#include "somemanagedclass.hpp"
#include <vcclr.h>
using namespace SomeNameSpace;
class IMPORT_EXPORT SomeNativeClass : public SomeNativeInterface
{
public:
SomeNativeClass();
~SomeNativeClass();
void CallbackInit(NativeCallback fnNativeCallbackInit); //can this take an instance?
private:
NativeCallback fnNativeCallback;
void OnNativeCallback(const wchar_t* cString, int iSize);
gcroot<SomeManagedClass^> wrapper; //warning C4251
};
Native wrapper source:
//somenativeclass.cpp
#include "somenativeclass.hpp"
#include <vcclr.h>
#include <string.h>
using namespace System;
using namespace Runtime::InteropServices;
SomeNativeInterface* SomeNativeInterface::Create()
{
return ((SomeNativeInterface*) new SomeNativeClass());
}
void SomeNativeInterface::Destroy(SomeNativeInterface* instance)
{
delete instance;
}
SomeNativeClass::SomeNativeClass()
{
wrapper = gcnew SomeManagedClass();
}
SomeNativeClass::OnNativeCallback(const wchar_t* cString, int iSize)
{
std::auto_ptr<char> pcConvertedString(new char[iSize+1]);
size_t iCharsConverted;
if (wcstombs_s(&iCharsConverted, (char*)*pcConvertedString, iSize+1, cString, iSize) == 0)
{
if (iCharsConverted > 0xFFFFFFFF)
iCharsConverted = 0xFFFFFFFF; //truncate
fnNativeCallback((const unsigned char*)*pcConvertedString, (unsigned long)(iCharsConverted));
}
}
SomeNativeClass::CallbackInit(NativeCallback fnNativeCallbackInit)
{
fnNativeCallback = fnNativeCallbackInit;
wrapper->RegisterCallback(System::IntPtr(this->OnNativeCallback)); //error C3867
}

Put the code from a FORM in a .cpp file

I have a form created in this way (the .h file of form is named Example):
This is a default code from a form:
namespace ExampleNamespace{
public ref class Example : public System::Windows::Forms::Form
{
public:
void DoSomething()
{
// code here
}
};
}
I can to this ?
Creating a new .cpp file and named Example.cpp am put here all the code from the form in .cpp ? In this way don't work.
namespace ExampleNamespace{
public ref class Example : public System::Windows::Forms::Form
{
public:
void DoSomething(); //if i try in this way I will have the error from above
};
}
and in Example.cpp
#include "Example.h"
void Example::DoSomething()
{
//code here
}
If I try to add public: void DoSomething(); in form i have this error:
error LNK2020: unresolved token (060000BB) ExampleNamespace.Example::DoSomething
I got the answer, you need to include the namespace after include the .h file like this in the .cpp file:
#include "Example.h"
using namespace ExampleNamespace;
And in *.h file you need to put this code:
#ifndef _EXAMPLE_H_
#define _EXAMPLE_H_
and
#endif
like this:
#ifndef _EXAMPLE_H_
#define _EXAMPLE_H_
namespace ExampleNamespace{
public ref class Example : public System::Windows::Forms::Form
{
public:
void DoSomething()
{
// code here
}
};
}
#endif

How to push QML page on button click in C++ code

I am developing one BB 10 app in which I have coded first page (with NavigationPane) in C++. Now I want to push another qml page in NavigationPane on button click. I have tried following code with no luck
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
if (!qml->hasErrors()) {
Page *page = qml->createRootObject<Page>();
mRoot->push(page);
}
How can I achieve this?
Here is how can you push page using NavigationPane in C++:
Source file:
#include <bb/cascades/Application>
#include <bb/cascades/Button>
#include <bb/cascades/Label>
#include <bb/cascades/ActionItem>
#include <bb/cascades/Container>
#include <bb/cascades/DockLayout>
#include <bb/cascades/TitleBar>
#include <bb/cascades/NavigationPaneProperties>
#include "Sandoxproject.hpp"
using namespace bb::cascades;
SandboxApp::SandboxApp(bb::cascades::Application *app)
: QObject(app)
{
_navPane.reset(NavigationPane::create());
Page* firstPage = createFirstPage();
_navPane ->push(firstPage);
_secondPage.reset(createSecondPage());
app->setScene(_navPane.data());
}
bb::cascades::Page* SandboxApp::createFirstPage() {
Page* page = new Page();
Container* content = new Container();
TitleBar* titleBar = TitleBar::create().visibility(ChromeVisibility::Visible).title("First Page");
page->setTitleBar(titleBar);
content->setLayout(DockLayout::create());
Button* button = Button::create().text("Go to another page").horizontal(HorizontalAlignment::Center).vertical(VerticalAlignment::Center);
connect(button, SIGNAL(clicked()), this, SLOT(pushPage()));
content->add(button);
page->setContent(content);
return page;
}
bb::cascades::Page* SandboxApp::createSecondPage() {
Page* page = new Page();
TitleBar* titleBar = TitleBar::create().visibility(ChromeVisibility::Visible).title("Second Page");
page->setTitleBar(titleBar);
ActionItem* backAction = ActionItem::create();
connect(backAction, SIGNAL(triggered()), _navPane.data(), SLOT(pop()));
page->setPaneProperties(NavigationPaneProperties::create().backButton(backAction));
Container* content = new Container();
content->setLayout(DockLayout::create());
content->add(Label::create().text("This is the second page").horizontal(HorizontalAlignment::Center).vertical(VerticalAlignment::Center));
page->setContent(content);
return page;
}
void SandboxApp::pushPage() {
qDebug("pushing another page...");
_navPane->push(_secondPage.data());
}
Header file:
#ifndef Sandoxproject_HPP_
#define Sandoxproject_HPP_
#include <bb/cascades/NavigationPane>
#include <bb/cascades/Page>
#include <QObject>
namespace bb { namespace cascades { class Application; }}
class SandboxApp : public QObject
{
Q_OBJECT
public:
SandboxApp(bb::cascades::Application *app);
virtual ~SandboxApp() {}
private slots:
void pushPage();
private:
bb::cascades::Page* createFirstPage();
bb::cascades::Page* createSecondPage();
QScopedPointer<bb::cascades::NavigationPane> _navPane;
QScopedPointer<bb::cascades::Page> _secondPage;
Q_DISABLE_COPY(SandboxApp);
};
#endif /* Sandoxproject_HPP_ */

Is it possible to point a Typedef function pointer to a class member?

I am working with an executable that includes a DLL. For my testcase, I combined the code into a single executable. I am working with Visual Studio 2008 and Boost 1.43. I've tried researching this, but haven't found any clear answer. Thanks for the help.
In my main.h:
#include <string>
//These are normally defined in a seperate DLL
typedef std::string Typedef_func(const std::string & title);
void Register_My_Typedef(Typedef_func*);
//-------------------------------------------
class myClass
{
public:
std::string func_one(const std::string & title);
Typedef_func _test;
void run();
};
In my main.cpp:
#include "main.h"
#include <boost/bind.hpp>
std::string workingFunc(const std::string & title)
{
return "";
}
int main(int argc, char* argv[])
{
myclass* example;
example->run();
Register_My_Typedef(&workingFunc);//This works.
return 0;
}
void myClass::run()
{
//I want to point a Typedef_func* in a DLL to call myclass::func_one
Typedef_func* tf = boost::bind(&myClass::func_one, this, "test"); //This does not.
Register_My_Typedef(tf);
}
std::string myClass::funcOne(const std::string & title)
{
return "";
}
void Register_My_Typedef(Typedef_func* passedIn)
{
//Points the pointer in the DLL to passedIn
}
The DLL logic works fine when Register_My_Typedef is called on a function not in a class, but is it possible to call it from within a class? When I try to compile this code it returns:
When I try and compile in Windows XP with VS2008 I get:
Error C2440: 'initializing' : cannot convert from
'boost::_bi::bind_t' to 'Typedef_func (__cdecl *)' with
[
R=std::string,
F=boost::_mfi::mf1,
L=boost::_bi::list2,boost::_bi::value>
]
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called.
The Answer is Typedef itself is class member for a static member function & behave differently to non-static however works best reading rather class function of user defined class main.