Invoke C/C++ code from C# - c#

I usually use C# but have the pleasure to invoke a C method via C++. So I wrote a C++ wrapper like this:
The C++ header file looks like this:
#pragma once
using namespace System;
namespace CSharpToCPP {
public ref class SomeWrapper
{
public:
double ReturnValue();
};
}
The C++ code looks like this:
double CSharpToCPP::SomeWrapper::ReturnValue()
{
return 1;
}
This gives me dll which I can reference in C#. The working code in C# looks like this:
class Program
{
static void Main(string[] args)
{
SomeWrapper Wrapper = new SomeWrapper();
Console.WriteLine(Wrapper.ReturnValue());
}
}
Now I would like to create some data in the C++ method ReturnValue and invoke the C method with this signature:
real_T get_value(const e_struct_T parameters, real_T temp)
Can someone please be so kind and point me in the right direction which explains what a const e_struct_T is and how to create such data? The C code was automatically generated and should take a structure with known content. I tried to define a structure in the header file like this:
struct Parameters{
double x;
int y;
char z1;
bool z2;
};
and then populate it with data like this:
Parameters Ps;
Ps.x = z2;
Ps.x = 2.0;
before invoking get_value like this:
get_value(Ps, 10.0);
This does not work. Thanks!

You must search for an header file containing the definition of e_struct_t, include in the calling file, and use a variable of that type to pass to get_value;
By the way, if the target method you want to call from C# is pure C, you probably should consider better using P/Invoke ( a tutorial here ) directly instead of create a C++ wrapper. Furthermore by plain P/Invoke you will drastically simplify the deploy ( no additional dll needed )

Related

What is the F# equivalent of C# named arguments?

In the DotNetYaml sample code I'm looking at, there's a C# construct:
var deserializer = new Deserializer(namingConvention: new CamelCaseNamingConvention());
var order = deserializer.Deserialize<Order>(input);
What is the equivalent F# code? I've tried
let deserializer = new Deserializer(namingConvention=new CamelCaseNamingConvention())
deserializer.Deserialize<Meta>(input)
If you have a C# library that defines optional parameters, then you can use the syntax you are using in your question. To quickly show that's the case, I compiled the following C# code as a library:
using System;
namespace Demo {
public class MyClass {
public static void Foo(int first, string second = "foo", string third = "bar") { }
}
}
You can reference this and use it from F# as follows:
open Demo
MyClass.Foo(1, third="hi")
I tried to do this with YamlDotNet which, I guess, is the library that you were using, but I get an error that the Deserializer class does not have namingConvention as an argument, so my guess would be that you are probably using a different version of the library than you are thinking (or perhaps, my guess of what library you're using was wrong...).

Creating an object inside a dll on heap to use in other dll calls

Background
I have a c# program that calls a c++ dll containing the code to create a model(Net type) and use the model to evaluate an image. As the dll function currently contains the code to load the model, the time taken to run this function is too long the for the task of evaluating a live feed.
I would like to only have to create the model once from a dll call and then pass a pointer to the model to an evaluate function, thus removing the overhead of loading the model from file.
Attempt
I have attempted to create a function that is passed a void **, creates a model on heap and points the passed void ** to it. This would allow me to pass the pointer to other functions within the dll and create a model object from the memory on heap.
I cannot get this to work and, although it seems to me like it should be possible, i am not sure if it is.
Any suggestions on how to tackle this or on how to fix my code would be appreciated, thanks.
Error
Attempted to read or write protected memory at "loadNet()"
c++/dll code
void loadNet(void **np)
{
String modelFile = "graph.pb";
Net *net;
net = (Net *)malloc(sizeof(Net));
*net = readNetFromTensorflow(modelFile)
*np = &net;
}
extern "C"{
DLLEXPORT void loadNet(void **);
}
c# code to call dll
[DllImport("Project5.dll")]
public static extern unsafe void loadNet(void **np);
static void Main(string[] args)
{
unsafe
{
void *np;
loadNet(&np);
Bitmap img = (Bitmap)Image.FromFile("2.png");
BitmapData bmpData = img.LockBits(new Rectangle(0,0,img.Width,
img.Height),ImageLockMode.ReadWrite,
img.PixelFormat);
Results results = evalImgGest2(bmpData.Scan0,&np);
img.UnlockBits(bmpData);
Console.WriteLine(results.five);
Console.WriteLine(results.fist);
Console.WriteLine(results.index);
}
}
Updates
net = (Net *)malloc(sizeof(Net)); // is causing error
Net *net = new Net(); // replacing with this removes error

How would I create a "type" of a Callable?

Is there a Pythonic way of creating a Callable type like below (TypeScript and C# examples)?
TypeScript:
export type MyHandler = (paramOne: ClassOne, num: int) => null;
C#:
public delegate void MyHandler(ClassOne paramOne, int num);
Python:
MyHandler = Callable[[ClassOne, int], None] # No lint errors...
While I'm not getting any lint errors in PyCharm; I haven't bothered running my snippet yet because of how not-Pythonic this feels. If this was going to be my own code for private consumption I don't think I would bother with creating this type, but it's going to be part of a library so I feel more obligated to create the type.
In usage, this type would show up in something like:
class SomeClass(ProbablyAnABC):
# __init__ where self._handlers is created
def register_handler(self, handler: MyHandler) -> None:
self._handlers.append(handler)

Ice Chat Application

I'm ICE starter. At http://zeroc.com there is good tutorial on how to create chat. I decided to use the tutorial as base. And first thing I tried to do was writing ChatRoom class in c# instead of given c++ implementation. I tried to do the same in my c# code. ChatRoom implementation in c++:
// C++
class ChatRoomCallbackAdapter { /* ... */ };
typedef IceUtil::Handle<ChatRoomCallbackAdapter> ChatRoomCallbackAdapterPtr;
class ChatRoom : public IceUtil::Shared
{
public:
ChatRoom(bool trace, const Ice::LoggerPtr& logger);
void reserve(const std::string&);
void unreserve(const std::string&);
void join(const std::string&, const ChatRoomCallbackAdapterPtr&);
void leave(const std::string&);
Ice::Long send(const std::string&, const std::string&);
private:
typedef std::map<std::string, ChatRoomCallbackAdapterPtr> ChatRoomCallbackMap;
ChatRoomCallbackMap _members;
std::set<std::string> _reserved;
IceUtil::Mutex _mutex;
const bool _trace;
const Ice::LoggerPtr _logger;
};
Some piece of class-members implementation:
// ...
void ChatRoom::reserve(const string& name)
{
IceUtil::Mutex::Lock sync(_mutex);
if(_reserved.find(name) != _reserved.end() ||
_members.find(name) != _members.end())
{
throw string("The name " + name + " is already in use.");
}
_reserved.insert(name);
}
// ...
I was writng next:
public class ChatRoom : IceUtil
when I encountered an error. I found that IceUtil dll in distribution package isn't COM-visible therefore I can't use it in my c# project.
What can I use instead of c++
IceUtil::Handle<T>
as far as I understand it is a smart pointer.
How can I implement server like the one's given in c#?
Would it be the same in c# (talking about mutexes) comparing to above c++ class:
public class ChatRoom
{
// ...
void Reserve(System.String Name)
{
lock(this)
{
// operations
}
}
}
?
Thanks!
I don't know anything about ICE, but their website lists a .NET implementation - why don't you use that instead of COM if you want to use C#? There's even a section of documentation with an example of a C# server.
C++ does not support reference counted pointers out of the box, that is why C++ API has IceUtil::Handle<> template. C# obviously does not need it. I'd recommend you start learning Ice for C# using C# examples rather than C++. You can find a lot of C# client/server examples in democs folder of demos packages. And, of course, Ice has absolutely nothing to do with COM technology, except that it is kind of a replacement.

IronRuby calling C# Extension Methods - Error - Compatibility in .NET 3.5

I have written an Extension Method off of DataGridView called HideColumns.
public static class Extensions
{
public static void HideColumns(this DataGridView dataGridView, params string[] columnNames)
{
foreach (string str in columnNames)
{
if (dataGridView.Columns[str] != null)
{
dataGridView.Columns[str].Visible = false;
}
}
}
}
I pass my grid into an IronRuby script as a variable called main_grid
When my script calls
main_grid.HideColumns("FirstName","LastName")
the script blows up with Error in Script
undefined method 'HideColumns' for System.Windows.Forms.DataGridView:System::Windows::Forms::DataGridView
The extension methods seem to work okay from C#. What gives?
FWIW, IronRuby 1.1 (needs .net 4) provides the using_clr_extensions method -- as noted in the release notes this activates all extension methods defined on classes defined in a given namespace, regardless of the assembly they are defined in; assemblies loaded in the future that define extension methods in the activated namespace will automatically appear on the correct types, like this:
load_assembly "System.Core"
using_clr_extensions System::Linq
# ...
products.
where(lambda { |p| p.units_in_stock == 0 }).
each { |x| puts x.product_name }
The release notes also point at a whole set of examples at http://github.com/ironruby/ironruby/blob/master/Languages/Ruby/Samples/Linq/101samples.rb
The extension method is just syntatic sugar, you will need to call it as:
Extensions.HideColumns(main_grid, "FirstName", "LastName")
alternatively create a new class in C# which derives from DataGridView and add the method:
public class DataGridViewExt : DataGridView
{
public void HideColumns(params string[] columnNames)
{
foreach (string str in columnNames)
{
if (this.Columns[str] != null)
{
this.Columns[str].Visible = false;
}
}
}
}
and use this class rather than the System.Windows.Forms class on your form.
Since you mentioned it in the comments to JDunkeryly's answer, here's how you'd extend the grid from the ruby side. Just open the class and add a method (only works from the ruby side).
class System::Windows::Forms::DataGridView
def hide_columns(*columnNames)
column_names.each do |cn|
self.columns[cn].visible = false
end
end
end
As far as the suggestion to use the extension method directly, the params keyword is painful to IronRuby. You need to build a typed array with your arguments and pass it. You can't just wrap your ruby strings in a ruby array. I've pulled this off earlier today in a blog post. But if you have a smoother way to handle that, please let me know.

Categories

Resources