[.net] VC++ Function called from C#

Started by
7 comments, last by Erzengeldeslichtes 17 years, 7 months ago
I woul dlike to know if it's "easy" to call a function writen in VC++ like this one :

public override unsafe void Write(byte[] buffer, int offset, int count)
{
      if (!this.canWrite)
      {
            throw new NotSupportedException();
      }
      if ((this.pData == null) || (this.dataSize == 0))
      {
            throw new InvalidOperationException();
      }
      long num1 = count;
      if ((this.currentPosition + num1) > this.dataSize)
      {
            throw new InvalidOperationException();
      }
      volatile ref byte  pinnedlocal1 = (volatile ref byte) &(buffer[offset]);
      memcpy(local1, (((int) this.currentPosition) + this.pData), count);
      this.currentPosition += num1;
}

From C# ? Is it possible ? Easily ? It could resolve the "missing" memcpy possibilities in C# ! ?
Advertisement
Compile the code into a dll.
Reference that dll in your C# project.
Call the function*

(I've never actually called a straight C++ function from C#. If it doesn't work, just wrap it into some class if at all possible.)

Edit: It seems like this function is already part of a class. You should be able to transparently use your class in C# code.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
Strange "problem" :

I write a simple CLR dll with VC++ express here is he code :

SeeMeTools.h File
#pragma onceusing namespace System;namespace SeeMeTools {	public class Memory	{		// TODO: Add your methods for this class here.	private: 		Memory(void);	public: 		static int MemCopy(int Value);	};}




.cpp file
#include "stdafx.h"#include "SeeMeTools.h"namespace SeeMeTools {	Memory::Memory(void){}	int Memory::MemCopy( int Value )	{		Value++;		return Value;	}}


Quand je référence la DLL d'un programme C#, le classe Memory est considérée comme une structure !

Here is the code retrieve from this dll with reflector :

[StructLayout(LayoutKind.Sequential, Size=1), CLSCompliant(false), NativeCppClass, MiscellaneousBits(0x40), DebugInfoInPDB]public struct Memory{}


Does someone know why Memory is seen as a struct when compiled ?

Tx you !
[/source]
Probably because it's not a class in the C# and .net sense. The C# class is called a "ref class" or "ref struct" in managed C++. Since both C# structs and C++ unmanaged classes can live on either the heap or stack, I guess it's seen as a struct that's marked as "unmanaged". After all, in C++, there's no significant differance between a struct and class while in C# and .Net there's a very massive difference.
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
Hoho Tx you !
Indead, I have learn the C++ before the .net "generation".
So I'm quite rusty with all those new things !

Declaring the class as public static ref class, make it seen as a Class in C# !

Tx you !

Sorry for interupting this thread. For large project, in c++ we can use #include however how to do it in c#. Do we have to compile the each c# to dll file and then import the file later? etc?

ps: Sorry for my bad English.
>--HELMI-->>
You really should just start a new thread.

But when you referance a DLL all public types are immediately available to all cs files in the assembly.

Within an assembly each cs file has complete access to the types in every other cs file.
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
Hello, On my way trying to write my .dll from VC++, I have 2 others problems :

void Memory::MemCopy(Array DataSrc, ...){    GCHandle handle = GCHandle::Alloc(DataSrc, GCHandleType::Pinned);    handle.Free();}


In this case I have 2 errors :
'System::Array': a class declared as 'abstract' cannot be instantiated And
'void SeeMeTools::Memory::MemCopy(System::Array,System::IntPtr,int,int)' : overloaded member function not found in 'SeeMeTools::Memory'

And even when I use a system::object instead of Array, I have this error :
'System::Runtime::InteropServices::GCHandle::Alloc' : none of the 2 overloads could convert all the argument types

Can someone help me ?

Tx you !

Firstly, in Managed C++ all ref classes must be GC Handles, ie, System::Array^ DataSrc. The second portion wants the predeclaration to match the definition. So you have to check to make sure the class Memory has a function declared which takes the System::Array^ and whatever else you chose.

As for the third portion, the GCHandle is expecting just that: A handle to a garbage collected object. You seem to be using System::Array and System::Object directly rather than through the requisite handles (System::Array^ and System::Object^). The compiler should be complaining loudly about this.
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?

This topic is closed to new replies.

Advertisement