C++ to C#

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Joey Wu (joeywu_at_ezmsg.com.tw)
Date: 06/26/04


Date: Sat, 26 Jun 2004 18:51:48 +0800

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



Relevant Pages

  • Re: Problem using SP_TRACE_SETFILTER - it doesnt seem to apply th
    ... @bigintfilter and @intfilter input parameters) because of the nature of the ... @duration int, -- in minutes ... 1.04 - customised to capture specific information for PrecisDM ... exec master.dbo.xp_cmdshell @RenameCmd,NO_OUTPUT ...
    (microsoft.public.sqlserver.server)
  • Re: Screen capture
    ... but i can provide you with some screen capture code: ... int nXDest, // x-coord of destination upper-left corner ...
    (microsoft.public.dotnet.csharp.general)
  • [PATCH] usbmon: control the max amount of captured data for eachurb
    ... An ioctl method is added to each usbmon text files. ... capture for each URB, and changing this value. ... char setup_flag; ... - int len, char ev_type) ...
    (Linux-Kernel)
  • Convert @@ERROR to fully qualified System Message?
    ... Declare @MyErrInt as int ... will capture the error number ... How do I convert the error number into the actual system error message for ... storage in a varchar ... ...
    (microsoft.public.sqlserver.programming)
  • Re: C++ to C#
    ... I think that is a very useful article. ... >> public class CCD ... >> unsafe internal static extern int DeviceIoControl( ...
    (microsoft.public.dotnet.framework.compactframework)