How to call a function from one project in another one? - visual-c++

I have a solution consisting of two projects:
Project1
containing a single file program.cpp
namespace Program1 {
void foo() { ... }
}
Project2
containing another single file program.cpp in which I'd like to call foo()
namespace Program2 {
void bar() { Program1::foo() }
}
I have set:
project dependencies (Program2 depends on Program1)
references in Program2 to Program1
But still I recieve an error
error C2653: 'Program1' : is not a class or namespace name
What else do I need to do to call Program1::foo() in Project2?

You need to include the header file which contains the declaration:
Project1 - Program.h
namespace Program1 {
void foo();
}
Project2 - Program.cpp
#include "Program.h"
namespace Program2 {
void bar() { Program1::foo() }
}

Related

Using Thread in in Mixed App C++ /cli to Call native Function in native class from managed class

I am working on mixed application, using both managed & native codes
I want to call a function deployed in a native class from Main() function located in Program.cpp which is managed class.
i tried using std::thread but failed with /cli
i tried to use Managed System::Threading::Thread but failed because i need to call a native function in a native class.
So how i can handle thing without using any third-party?
If you start from a native project you need to do the following steps:
Select project properties and change the option "No Common Language Runtime Support" to "Common Language Runtime Support /clr".
Open the "Property Manager" from "View" menu / "Other Windows" and add the property sheet "C++ Common Language Runtime Support" to the needed configuration (eg.: Debug | Win32) on my system this sheet is under "C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140". I use the "Microsoft.Cpp.ManagedExtensions.props" file.
You need to remove std::thread completely.
headerish:
#pragma once
#include<stddef.h>
using namespace System;
using namespace System::Threading;
namespace FooSpace
{
// Native stuff
public class Native
{
public:
static void Foo() { }
void Bar() {
}
};
// Managed stuff
public ref class Managed
{
private:
Native* m_Native;
public:
Managed()
{
m_Native = new Native();
}
~Managed()
{
if (NULL != m_Native)
{
delete m_Native;
m_Native = NULL;
}
}
void Bar()
{
m_Native->Bar();
}
static void ThreadCall(Object^ o)
{
auto me = (Managed^)o;
me->Bar(); // Call a method of an instance of the native class
Native::Foo(); // Call a static method of the Native class
}
void StartThread()
{
auto t = gcnew Thread(gcnew ParameterizedThreadStart(ThreadCall));
t->Start(this);
t->Join();
}
};
}
soure file:
#include "stdafx.h"
#include "CppCli_Native.h"
using namespace FooSpace;
int main()
{
Native::Foo(); // call native static method
auto native = new Native(); // create native instance
native->Bar(); // call native method
auto managed = gcnew Managed();
managed->Bar(); // This will call bar
managed->StartThread(); // This will start a thread
delete managed;
Console::ReadLine();
return 0;
}
Edit: It turns out that you don't need to use IntPtr to store native class.
I find this answer also useful, it also gives us a fast introduction to c++-cli syntax.

How to call java function and pass arguments from c++ using Android NDK

I try to call a java function from my c++ code, but the app keeps 'crashing'.
At first, I start the c++ code through JNI call, which works without any problem. Then I let the function which is called executing the callback:
#include <jni.h>
extern "C"
JNIEXPORT jstring JNICALL
Java_net_example_folder_Service_startSomething(JNIEnv *env, jobject obj) {
invoke_class(env);
return env->NewStringUTF("The End.\n"); //works if I only use this line
}
Trying to follow http://www.inonit.com/cygwin/jni/invocationApi/c.html (and a lot of other guides/tips etc.), I use this to call the java function:
void invoke_class(JNIEnv* env) {
jclass helloWorldClass;
jmethodID mainMethod;
helloWorldClass = env->FindClass("Java/net/example/folder/helloWorldClass");
mainMethod = env->GetStaticMethodID(helloWorldClass, "helloWorld", "()V");
env->CallStaticVoidMethod(helloWorldClass, mainMethod);
}
To call the java code:
package net.example.folder;
import android.util.Log;
public class helloWorldClass {
public static void helloWorld() {
Log.e("helloWorldCLass", "Hello World!");
}
}
The c++ code is called by a background service. Here is the function of the Activity that starts it:
public void startService() {
Intent i = new Intent(this, Service.class);
startService(i);
}
And this is a part of the Service:
public class SimService extends IntentService {
...
#Override
protected void onHandleIntent(Intent intent) {
System.loadLibrary("native-lib");
startSomething();
}
}
That all works, but when I now change the function 'invoke_class' to:
void invoke_class(JNIEnv* env) {
jclass helloWorldClass;
jmethodID mainMethod;
helloWorldClass = env->FindClass("net/example/folder/helloWorldClass");
mainMethod = env->GetStaticMethodID(helloWorldClass, "helloWorld", "([Ljava/lang/String;)V");
env->CallStaticVoidMethod(helloWorldClass, mainMethod, env->NewStringUTF("some text"));
}
and of course the java part to:
package net.example.folder;
import android.util.Log;
public class helloWorldClass {
public static void helloWorld(String msg) {
Log.e("helloWorldCLass", msg);
}
}
With that, I'll get the earlier mentioned crash.
Why is that? How do I pass arguments correctly?
Symbol [ is used to represent arrays (as if you had String[] msg), remove that from your GetStaticMethodID method.
You can see more information about JNI Types and Data Structures here
Also as Michael said - you should check for java exceptions in your native code, because otherwise your app will crash. You can do it using Exception jni functions. For example:
jclass exClass = env->FindClass("some/random/class");
if (exClass == nullptr || env->ExceptionOccurred()) {
env->ExceptionClear();
// do smth in case of failure
}

Local abstract class gives warning C4101

I have to deal with code that can be reduced as follows:
void main ()
{
class MyBase
{
public:
virtual void MyFunc () = 0;
};
class MyClass : public MyBase
{
public:
virtual void MyFunc () {}
};
MyClass x;
}
When compiling under Visual-C++ 2010 with `cl /nologo /Wall text.cxx', I get the following warning:
test.cxx(7) : warning C4101: 'main::MyBase::MyFunc' : unreferenced local variable
I want to keep MyBase in place if possible (because it is deep down inside the code hierarchy).
Is there a way to 'fix' the code in place (other than using #pragma to ignore the warning)? Or is it bad practice to have a local abstract class?
Thanks in advance!

Class members of a native object accessed using JNI change value unexpectedly

I have a Java project in which I use some C++ code using JNI.
I encountered a weird problem.
I have a class that looks more or less like so:
class MyClass
{
private:
MyType* _p;
public:
MyClass();
virtual ~MyClass();
void myFunc();
};
And:
MyClass::MyClass() : _p(NULL) {
// _p's value here is indeed NULL (0)
}
MyClass::~MyClass() {
}
void MyClass::myFunc() {
if (_p != NULL) {
delete _p;
}
_p = new MyType();
}
No other function but myFunc touches _p, and for some reason, even after initializing it to NULL, when calling myFunc for the first time, _p has some garbage value in it and the function attempts to delete it.
The ctor of MyClass is called using JNI, and myFunc is too called using JNI, on a separate occasion.
Any help would be greatly appreciated.

Compile Using MONO - C# with Reference To A C# Library?

I have a C# library (DLL)
// ProgramLib.cs //
using System;
namespace ProgramLibrary
{
public class Lib
{
public Lib()
{
Console.WriteLine("Lib Created");
}
}
}
And I have the following console program
// Program.cs //
using System;
using ProgramLibrary;
class MainClass
{
public static void Main (string[] args)
{
ProgramLibrary.Lib lib = new ProgramLibrary.Lib();
}
}
In a linux environment, if both files reside in the same directory
What is the Mono compiler (mcs) command that compiles Program.cs with reference to ProgramLib.cs?
Thanks all!!
First compile ProgramLib to ProgramLib.dll, then reference it:
$ gmcs -t:library ProgramLib.cs
$ gmcs -r:ProgramLib.dll Program.cs

Resources