C++ to C#

  • Thread starter Thread starter Joey Wu
  • Start date Start date
J

Joey Wu

Dear All.

I have a C++ application that can capture ccd image.
Now I tried to write a c# class to translate it then I can call from vb.net.
There is no any compiler error but it can`t return anything.
Please help and thanks a lot.

==========
C++ Sample
==========
#define VBUFSIZE 512*480*2
struct video_mmap
{
DWORD frame; //Reserved, no use here
WORD height,width; //specify the height and width to capture
DWORD format; //RGB15 = 6 //rgb16 = 3
};
typedef struct Capture {
BOOL Rflag, Wflag; //Reserved, not used.
BYTE ioctlBUF[VBUFSIZE]; //capture buffer passed to driver, returned
with captured image data
} BTVCapture;

hFile = CreateFile(L"BTV0:", GENERIC_WRITE |GENERIC_READ, 0, NULL,
OPEN_EXISTING, 0, NULL);

int input;
input = 0;
DeviceIoControl(hFile, BT_IOCTL_INPUT, &input, sizeof(input), &tmp,
sizeof(tmp), NULL, NULL);
DeviceIoControl(hFile, VIDIOCSYNC, &vm, sizeof(vm), CAP.ioctlBUF,
sizeof(CAP.ioctlBUF), NULL, NULL);


===========
My C# Class
===========
public class CCD
{
const int VBUFSIZE = 520*480*2;
const int VIDIOCSYNC = 1026;
const int BT_IOCTL_INPUT =1029;
const int BT_IOCTL_BRIGHT =1030;
const int BT_IOCTL_CONTRAST =103;

struct video_mmap
{
public uint frame; //Reserved, no use here
public ushort height,width; //specify the height and width to capture
public uint format; //RGB15 = 6 //rgb16 = 3

public video_mmap(int myFrame, int myHeight, int myWidth, int myFormat)
{
frame = System.Convert.ToUInt32(myFrame);
height = System.Convert.ToUInt16(myHeight);
width = System.Convert.ToUInt16(myWidth);
format = System.Convert.ToUInt32(myFormat);
}
}

[DllImport("coredll.dll", EntryPoint="DeviceIoControl",
SetLastError=true)]
unsafe internal static extern int DeviceIoControl(
IntPtr hDevice,
int dwIoControlCode,
void * lpInBuffer,
int nInBufferSize,
void * lpOutBuffer,
int nOutBufferSize,
string lpBytesReturned,
string lpOverlapped);

unsafe public static int sdCmd(IntPtr hDevice,int Channel)
{
int sChannel = Channel;
IntPtr sDev = hDevice;
int Ret;
int tmp;
Ret = DeviceIoControl(sDev, BT_IOCTL_INPUT, &sChannel, sizeof(int), &tmp,
sizeof(int), null, null);
return Ret;
}

unsafe public static byte[] sdCmd(IntPtr hDevice, int Height, int Width)
{
video_mmap vm = new video_mmap(0, 240, 320, 3);

byte[] myBuf = new byte[VBUFSIZE];
fixed (void* p = myBuf)
{
DeviceIoControl(hDevice, VIDIOCSYNC, &vm, sizeof(video_mmap), p,
VBUFSIZE, null, null);
return myBuf;
}
}
}

==============
I call from vb.net
==============

Dim btvHandle As IntPtr = FileEx.CreateFile("BTV0:",
DirectCast(CType(Convert.ToUInt32(OpenNETCF.IO.FileAccess.All), Object),
OpenNETCF.IO.FileAccess), OpenNETCF.IO.FileShare.None,
FileCreateDisposition.OpenExisting, vbNullString)

Dim myCap As DeviceControl.CCD = New DeviceControl.CCD

Try
myCap.sdCmd(btvHandle, 0)
Dim myPic As MemoryStream = New
MemoryStream(myCap.sdCmd(btvHandle, 240, 320))
picRec.Image = New Bitmap(myPic)
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
 
Take a look at my article. It could help with marshaling of your structs :
http://www.opennetcf.org/PermaLink.aspx?guid=abe6694a-ff15-4617-a21f-8163d427a89c

--
Alex Yakhnin, NET CF MVP
IntelliProg, Inc.
http://www.intelliprog.com

Joey Wu said:
Dear All.

I have a C++ application that can capture ccd image.
Now I tried to write a c# class to translate it then I can call from vb.net.
There is no any compiler error but it can`t return anything.
Please help and thanks a lot.

==========
C++ Sample
==========
#define VBUFSIZE 512*480*2
struct video_mmap
{
DWORD frame; //Reserved, no use here
WORD height,width; //specify the height and width to capture
DWORD format; //RGB15 = 6 //rgb16 = 3
};
typedef struct Capture {
BOOL Rflag, Wflag; //Reserved, not used.
BYTE ioctlBUF[VBUFSIZE]; //capture buffer passed to driver, returned
with captured image data
} BTVCapture;

hFile = CreateFile(L"BTV0:", GENERIC_WRITE |GENERIC_READ, 0, NULL,
OPEN_EXISTING, 0, NULL);

int input;
input = 0;
DeviceIoControl(hFile, BT_IOCTL_INPUT, &input, sizeof(input), &tmp,
sizeof(tmp), NULL, NULL);
DeviceIoControl(hFile, VIDIOCSYNC, &vm, sizeof(vm), CAP.ioctlBUF,
sizeof(CAP.ioctlBUF), NULL, NULL);


===========
My C# Class
===========
public class CCD
{
const int VBUFSIZE = 520*480*2;
const int VIDIOCSYNC = 1026;
const int BT_IOCTL_INPUT =1029;
const int BT_IOCTL_BRIGHT =1030;
const int BT_IOCTL_CONTRAST =103;

struct video_mmap
{
public uint frame; //Reserved, no use here
public ushort height,width; //specify the height and width to capture
public uint format; //RGB15 = 6 //rgb16 = 3

public video_mmap(int myFrame, int myHeight, int myWidth, int myFormat)
{
frame = System.Convert.ToUInt32(myFrame);
height = System.Convert.ToUInt16(myHeight);
width = System.Convert.ToUInt16(myWidth);
format = System.Convert.ToUInt32(myFormat);
}
}

[DllImport("coredll.dll", EntryPoint="DeviceIoControl",
SetLastError=true)]
unsafe internal static extern int DeviceIoControl(
IntPtr hDevice,
int dwIoControlCode,
void * lpInBuffer,
int nInBufferSize,
void * lpOutBuffer,
int nOutBufferSize,
string lpBytesReturned,
string lpOverlapped);

unsafe public static int sdCmd(IntPtr hDevice,int Channel)
{
int sChannel = Channel;
IntPtr sDev = hDevice;
int Ret;
int tmp;
Ret = DeviceIoControl(sDev, BT_IOCTL_INPUT, &sChannel, sizeof(int), &tmp,
sizeof(int), null, null);
return Ret;
}

unsafe public static byte[] sdCmd(IntPtr hDevice, int Height, int Width)
{
video_mmap vm = new video_mmap(0, 240, 320, 3);

byte[] myBuf = new byte[VBUFSIZE];
fixed (void* p = myBuf)
{
DeviceIoControl(hDevice, VIDIOCSYNC, &vm, sizeof(video_mmap), p,
VBUFSIZE, null, null);
return myBuf;
}
}
}

==============
I call from vb.net
==============

Dim btvHandle As IntPtr = FileEx.CreateFile("BTV0:",
DirectCast(CType(Convert.ToUInt32(OpenNETCF.IO.FileAccess.All), Object),
OpenNETCF.IO.FileAccess), OpenNETCF.IO.FileShare.None,
FileCreateDisposition.OpenExisting, vbNullString)

Dim myCap As DeviceControl.CCD = New DeviceControl.CCD

Try
myCap.sdCmd(btvHandle, 0)
Dim myPic As MemoryStream = New
MemoryStream(myCap.sdCmd(btvHandle, 240, 320))
picRec.Image = New Bitmap(myPic)
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
 
Thank you Alex~
I think that is a very useful article.
But it`s too hard to me. I am not familiar with c language.
So can you post another sample with in vb code?

I tried simply convert a structure to byte array.
It`s working in wince 3.0 But faild in .net ~~> <~~
Thanks a lot for your help.

Alex Yakhnin said:
Take a look at my article. It could help with marshaling of your structs :
http://www.opennetcf.org/PermaLink.aspx?guid=abe6694a-ff15-4617-a21f-8163d427a89c

--
Alex Yakhnin, NET CF MVP
IntelliProg, Inc.
http://www.intelliprog.com

Joey Wu said:
Dear All.

I have a C++ application that can capture ccd image.
Now I tried to write a c# class to translate it then I can call from vb.net.
There is no any compiler error but it can`t return anything.
Please help and thanks a lot.

==========
C++ Sample
==========
#define VBUFSIZE 512*480*2
struct video_mmap
{
DWORD frame; //Reserved, no use here
WORD height,width; //specify the height and width to capture
DWORD format; //RGB15 = 6 //rgb16 = 3
};
typedef struct Capture {
BOOL Rflag, Wflag; //Reserved, not used.
BYTE ioctlBUF[VBUFSIZE]; //capture buffer passed to driver, returned
with captured image data
} BTVCapture;

hFile = CreateFile(L"BTV0:", GENERIC_WRITE |GENERIC_READ, 0, NULL,
OPEN_EXISTING, 0, NULL);

int input;
input = 0;
DeviceIoControl(hFile, BT_IOCTL_INPUT, &input, sizeof(input), &tmp,
sizeof(tmp), NULL, NULL);
DeviceIoControl(hFile, VIDIOCSYNC, &vm, sizeof(vm), CAP.ioctlBUF,
sizeof(CAP.ioctlBUF), NULL, NULL);


===========
My C# Class
===========
public class CCD
{
const int VBUFSIZE = 520*480*2;
const int VIDIOCSYNC = 1026;
const int BT_IOCTL_INPUT =1029;
const int BT_IOCTL_BRIGHT =1030;
const int BT_IOCTL_CONTRAST =103;

struct video_mmap
{
public uint frame; //Reserved, no use here
public ushort height,width; //specify the height and width to capture
public uint format; //RGB15 = 6 //rgb16 = 3

public video_mmap(int myFrame, int myHeight, int myWidth, int myFormat)
{
frame = System.Convert.ToUInt32(myFrame);
height = System.Convert.ToUInt16(myHeight);
width = System.Convert.ToUInt16(myWidth);
format = System.Convert.ToUInt32(myFormat);
}
}

[DllImport("coredll.dll", EntryPoint="DeviceIoControl",
SetLastError=true)]
unsafe internal static extern int DeviceIoControl(
IntPtr hDevice,
int dwIoControlCode,
void * lpInBuffer,
int nInBufferSize,
void * lpOutBuffer,
int nOutBufferSize,
string lpBytesReturned,
string lpOverlapped);

unsafe public static int sdCmd(IntPtr hDevice,int Channel)
{
int sChannel = Channel;
IntPtr sDev = hDevice;
int Ret;
int tmp;
Ret = DeviceIoControl(sDev, BT_IOCTL_INPUT, &sChannel, sizeof(int), &tmp,
sizeof(int), null, null);
return Ret;
}

unsafe public static byte[] sdCmd(IntPtr hDevice, int Height, int Width)
{
video_mmap vm = new video_mmap(0, 240, 320, 3);

byte[] myBuf = new byte[VBUFSIZE];
fixed (void* p = myBuf)
{
DeviceIoControl(hDevice, VIDIOCSYNC, &vm, sizeof(video_mmap), p,
VBUFSIZE, null, null);
return myBuf;
}
}
}

==============
I call from vb.net
==============

Dim btvHandle As IntPtr = FileEx.CreateFile("BTV0:",
DirectCast(CType(Convert.ToUInt32(OpenNETCF.IO.FileAccess.All), Object),
OpenNETCF.IO.FileAccess), OpenNETCF.IO.FileShare.None,
FileCreateDisposition.OpenExisting, vbNullString)

Dim myCap As DeviceControl.CCD = New DeviceControl.CCD

Try
myCap.sdCmd(btvHandle, 0)
Dim myPic As MemoryStream = New
MemoryStream(myCap.sdCmd(btvHandle, 240, 320))
picRec.Image = New Bitmap(myPic)
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
 
You can compile the CustomMarshaler to the separate assembly and use it from
VB.NET or you can use one of the available VB to C# converters to try to
convert it to VB.NET.

--
Alex Yakhnin .NET CF MVP
www.intelliprog.com | www.opennetcf.org

NewBie said:
Thank you Alex~
I think that is a very useful article.
But it`s too hard to me. I am not familiar with c language.
So can you post another sample with in vb code?

I tried simply convert a structure to byte array.
It`s working in wince 3.0 But faild in .net ~~> <~~
Thanks a lot for your help.

Alex Yakhnin said:
Take a look at my article. It could help with marshaling of your structs :
http://www.opennetcf.org/PermaLink.aspx?guid=abe6694a-ff15-4617-a21f-8163d427a89c
--
Alex Yakhnin, NET CF MVP
IntelliProg, Inc.
http://www.intelliprog.com

Joey Wu said:
Dear All.

I have a C++ application that can capture ccd image.
Now I tried to write a c# class to translate it then I can call from vb.net.
There is no any compiler error but it can`t return anything.
Please help and thanks a lot.

==========
C++ Sample
==========
#define VBUFSIZE 512*480*2
struct video_mmap
{
DWORD frame; //Reserved, no use here
WORD height,width; //specify the height and width to capture
DWORD format; //RGB15 = 6 //rgb16 = 3
};
typedef struct Capture {
BOOL Rflag, Wflag; //Reserved, not used.
BYTE ioctlBUF[VBUFSIZE]; //capture buffer passed to driver, returned
with captured image data
} BTVCapture;

hFile = CreateFile(L"BTV0:", GENERIC_WRITE |GENERIC_READ, 0, NULL,
OPEN_EXISTING, 0, NULL);

int input;
input = 0;
DeviceIoControl(hFile, BT_IOCTL_INPUT, &input, sizeof(input), &tmp,
sizeof(tmp), NULL, NULL);
DeviceIoControl(hFile, VIDIOCSYNC, &vm, sizeof(vm), CAP.ioctlBUF,
sizeof(CAP.ioctlBUF), NULL, NULL);


===========
My C# Class
===========
public class CCD
{
const int VBUFSIZE = 520*480*2;
const int VIDIOCSYNC = 1026;
const int BT_IOCTL_INPUT =1029;
const int BT_IOCTL_BRIGHT =1030;
const int BT_IOCTL_CONTRAST =103;

struct video_mmap
{
public uint frame; //Reserved, no use here
public ushort height,width; //specify the height and width to capture
public uint format; //RGB15 = 6 //rgb16 = 3

public video_mmap(int myFrame, int myHeight, int myWidth, int myFormat)
{
frame = System.Convert.ToUInt32(myFrame);
height = System.Convert.ToUInt16(myHeight);
width = System.Convert.ToUInt16(myWidth);
format = System.Convert.ToUInt32(myFormat);
}
}

[DllImport("coredll.dll", EntryPoint="DeviceIoControl",
SetLastError=true)]
unsafe internal static extern int DeviceIoControl(
IntPtr hDevice,
int dwIoControlCode,
void * lpInBuffer,
int nInBufferSize,
void * lpOutBuffer,
int nOutBufferSize,
string lpBytesReturned,
string lpOverlapped);

unsafe public static int sdCmd(IntPtr hDevice,int Channel)
{
int sChannel = Channel;
IntPtr sDev = hDevice;
int Ret;
int tmp;
Ret = DeviceIoControl(sDev, BT_IOCTL_INPUT, &sChannel, sizeof(int), &tmp,
sizeof(int), null, null);
return Ret;
}

unsafe public static byte[] sdCmd(IntPtr hDevice, int Height, int Width)
{
video_mmap vm = new video_mmap(0, 240, 320, 3);

byte[] myBuf = new byte[VBUFSIZE];
fixed (void* p = myBuf)
{
DeviceIoControl(hDevice, VIDIOCSYNC, &vm, sizeof(video_mmap), p,
VBUFSIZE, null, null);
return myBuf;
}
}
}

==============
I call from vb.net
==============

Dim btvHandle As IntPtr = FileEx.CreateFile("BTV0:",
DirectCast(CType(Convert.ToUInt32(OpenNETCF.IO.FileAccess.All),
Object),
 
Back
Top