i am new to visual c++.. I had a method in .h file something like this:
public:
void DoSomething();
Here i need to pass byte array as parameter and i need to implement it in .cpp file.. I am working on windows phone 8 for this i need to include visual c++ project of windows phone run time component. I need to use this method in c# class and pass the byte array from there. But i dont know how to declare a byte array method in c++. can any one please help me to find the solution.
In C++/CX, what you're using for a Runtime Component, the signature would look like this (assuming you have a ref class):
void DoSomething(const Platform::Array<uint8>^ something);
This could be called from C# directly by passing in a byte[].
public:
void DoSomething(Byte *);
Or
public:
void DoSomething(unsigned char *);
for exemple.
Related
I'm writing a program that has a portable C++ backend that we want to create a WinRT Xaml frontend for. I want to keep our models in pure C++ code and create a view in C++/CX that mirrors the C++ model that we can pass around and consume on the frontend. Is there any standard method for this?
For example:
class Person
{
public:
const std::wstring GetName() const;
void SetName(const std::wstring& value);
private:
std::wstring m_Name;
}
ref class PersonMirror : INotifyPropertyChanged
{
public:
property Platform::String^ Name
{
Platform::String^ get();
void set(Platform::String^ value);
}
private:
std::shared_ptr<Person> m_Person;
}
Some things I don't know how to do in this are:
How I get the associated ref mirror class from the native class. can I convert a void* to a PersonMirror^ and talk to the GC about holding a reference to the object?
Are there C++ implementations for triggering events? Or I guess if I can maintain that this is a 1-to-1 relationship, I can just use a function pointer perhaps?
You cannot simply convert a void* to a PersonMirror^. You have to use "ref new" to create a WinRT instance of the class and then copy the data (or a pointer to it) from Person into the PersonMirror. You can have a PersonMirror constructor that copies individual fields from the ISO C++ class, or you can just copy a shared_ptr as you have done, and provide public accessors for it in your ref class. This is a nice approach. Note: There is no "GC" in C++/CX; a ref class is basically just a smart pointer.
• You can use whatever eventing mechanism you like between a ref class and an ISO C++ class in the same process. The only restriction is that any public types in the ref class must be WinRT compatible. To expose a callback or function object event handler in PersonMirror, give it internal accessibilty so that it is invisible to the WinRT interface, but your ISO code can still access it.
The Reversi sample on MSDN demonstrates an ISO C++ class that is wrapped by a C++/CX wrapper class.
By the looks of Google it seems like this might not be possible, but:
How do I define an 'out' parameter in a C++/CX 'ref class'?
If your answer is that this isn't possible, please provide a reference.
Any parameter which is of type T* (where T is a ABI-legal type) will be treated by the compiler as an out parameter, and decorated in metadata as such. The following code:
namespace TestMakePublic {
public ref class Class1 sealed
{
public:
void foo(int* out1, Object^* out2){}
};
}
Produces a function in metadata which looks like this (ildasm output):
.method public hidebysig newslot virtual final
instance void foo([out] int32& out1,
[out] object& out2) runtime managed
{
.override TestMakePublic.__IClass1PublicNonVirtuals::foo
} // end of method Class1::foo
Note that WinRT does not support "in/out" parameters, so the value of out1 and out2 are only valid for returning from the function, and cannot be trusted as inputs to foo.
It is a C# specific keyword, COM has it too in the IDL syntax. The equivalent in MSVC++ is the [out] attribute.
But no, the compiler is going to reject that with C3115 if you try to use it. Keep in mind that you use the C++/CX language extension to write code that's used by other languages. Which in general support to notion of [out] very poorly. Neither C++, Javascript or .NET languages like vb.net support it. You can see this as well in the .h files in C:\Program Files (x86)\Windows Kits\8.0\Include\WinRT, generated from the .idl files in that same directory that does have the [out] attribute. It was stripped in the .h file by midl.
It doesn't matter anyway since your code will be used in-process so there's no benefit at all from [out] being able to optimize the marshaling of the argument value. Just a plain pointer gets the job done. Having to initialize the argument value in C# code is however inevitable lossage.
You can use:
_Out_opt_
_Out_
But these are available only for private, internal, and protected members AFAIK.
I'm interested in calling a C# method from C++ code in Windows Phone 8. I have already learned how to pass a callback function to C++ code from C# via delegate declarations in my C++ code, but I am looking to see if I can do any of the following:
Call certain methods directly from the C++ code. This would involve somehow inspecting the C# object makeup from C++, and seems unlikely to me, but I thought I'd ask you all anyway
Trigger events in the C# code, which can then be handled by C# methods
Use a dispatcher to call C# callbacks in the Main UI thread so that the callbacks can modify UI elements
Use a dispatcher to trigger events in the C# code, (Essentially a merging of the above two points)
In short, I am looking for as many C++ -->C# communication tips as you guys can throw me, I want to learn it all. :)
By getting an object in C# code to implement a Windows RT interface, and passing down a reference to this object, it is possible to do all of the above with a bit of set-up (if I understand correctly - not sure about exactly what you want to do with your Dispatcher examples - you might want to wrap the Dispatcher on the C# side).
Create a Windows Runtime component library.
Define a public interface class in a C++/CX header for the C# to implement (C++ to call) (e.g. ICallback).
Define a public ref class in a C++/CX header for the C++ to implement (C# to call) (e.g. CppCxClass).
Add a method in CppCxClass that passes and stores an ICallback. (A C++ global variable is shown for consiseness, I recommend you review this to see if you can find a better place to store this in your code-base).
ICallback^ globalCallback;
...
void CppCxClass::SetCallback(ICallback ^callback)
{
globalCallback = callback;
}
Reference the WinRT library in your C# code.
C# code: create an instance of CppCxClass using var cppObject = new CppCxClass().
C# code: create a class which implements ICallback (e.g. CSharpCallbackObject).
C# code: pass an instance of CSharpCallbackObject down to C++. E.g. cppObject.SetCallback(new CSharpCallbackObject()).
You can now call C# with globalCallback->CallCsharp(L"Hello C#");. You should be able to extend either ICallback and/or CppCxObject to do the rest of your tasks.
After a lot of headaches trying to figure out the required code, I think it's worth posting the final version here
C++/CX
//.h
[Windows::Foundation::Metadata::WebHostHidden]
public interface class ICallback
{
public:
virtual void Exec( Platform::String ^Command, Platform::String ^Param);
};
//.cpp
ICallback ^CSCallback = nullptr;
void Direct3DInterop::SetCallback( ICallback ^Callback)
{
CSCallback = Callback;
}
//...
if (CSCallback != nullptr)
CSCallback->Exec( "Command", "Param" );
C#
public class CallbackImpl : ICallback
{
public void Exec(String Command, String Param)
{
//Execute some C# code, if you call UI stuff you will need to call this too
//Deployment.Current.Dispatcher.BeginInvoke(() => {
// //Lambda code
//}
}
}
//...
CallbackImpl CI = new CallbackImpl();
D3DComponent.SetCallback( CI);
I have been studying this method of API hooking using the mechanisms for hotpatching in windows dlls.
http://www.codeproject.com/KB/winsdk/0xF9EB_Hooking.aspx
I was wondering if anyone would know of a way to extend that to hooking non exported functions such as a C++ constructor for an internal class inside of a DLL. I have already know the address via dis-assembly... the problem I am having is how to set up the right calling conventions so that I can call the original function inside of my hook function.
I'm already to the point to where my hook function gets called... the program crashes because I can't return the results of calling the original function.
Lets assume we are talking about hooking an internal class constructor with a prototype something like this:
public __thiscall <class_name>::<class_name>(<single pointer arg to another object>)
depending on how your module is loaded, you can generally just overwrite the relative or absolute addresses at their respective call sites, else you need to make a trampolining function, for which its easier to use something like MS Detours.
In terms of the correct prototype for __thiscall based class member functions, you need some trickery, as you can't generally use __thiscall outside classes. The fastest and easiest way is to use __fastcall and ignore the second parameter. So your first definition becomes void __fastcall myctor(myobj* pObj).
Define it as a typical __stdcall function except that you'll have this pointer in ecx register. If you need this pointer, then use the __asm keyword to get the value:
void __stdcall HookedConstructor( SomeObject *pObject){
HookedClass *pClass;
__asm mov pClass, ecx;
...
}
Note that you'll have to do this at the beginning of the call. Otherwise, the value of ecx register may be overwritten.
In cuda we can create header files with .cuh extension and we can call the functions from anywhere like,
__device__ void doSomething()
{
....................
}
void doSomthingOnHost()
{
....................
}
these two functions are public. How can i make the host function to private?
I find that what works best for me is to
Make .CU files with my CUDA kernels, their public C/C++ wrappers and any private/encapsulated C/C++ functions I need to make the device code work.
Make .H files which provide access to the C/C++ wrappers inside my .CU files, #including them in the .CU files and any .C/.CPP files I need to call the device code from
Make .C/.CPP files which handle the high-level application logic and which invoke device code through interfaces supplied through the header files described in step 2.
To make host functions private in this scheme, just don't put prototypes for them in the header... a pretty neat scheme if you ask me.
Strictly speaking, there's no way to make a free function private in C++ -- any client which can see a function's signature can call it.
Instead, you could make doSomethingOnHost a private, static member function of some class:
class my_class
{
private:
static void doSomethingOnHost(); // only my_class or friends of my_class may use this function
};