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;
}
[.net] VC++ Function called from C#
I woul dlike to know if it's "easy" to call a function writen in VC++
like this one :
From C# ?
Is it possible ? Easily ?
It could resolve the "missing" memcpy possibilities in C# ! ?
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.
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.
Strange "problem" :
I write a simple CLR dll with VC++ express here is he code :
SeeMeTools.h File
.cpp file
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 :
Does someone know why Memory is seen as a struct when compiled ?
Tx you !
[/source]
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.
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 !
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.
ps: Sorry for my bad English.
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.
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.
Hello, On my way trying to write my .dll from VC++, I have 2 others problems :
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 !
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement