Click here to Skip to main content
15,887,676 members
Home / Discussions / C#
   

C#

 
GeneralRe: Most efficient Collection? Pin
Dalek Dave13-Jan-11 13:18
professionalDalek Dave13-Jan-11 13:18 
GeneralRe: Most efficient Collection? Pin
Luc Pattyn13-Jan-11 13:23
sitebuilderLuc Pattyn13-Jan-11 13:23 
AnswerRe: Most efficient Collection? Pin
jschell14-Jan-11 10:09
jschell14-Jan-11 10:09 
QuestionHaving trouble writing to an excel file Pin
turbosupramk313-Jan-11 9:40
turbosupramk313-Jan-11 9:40 
AnswerRe: Having trouble writing to an excel file Pin
Dalek Dave13-Jan-11 10:31
professionalDalek Dave13-Jan-11 10:31 
QuestionDuplicate media player winodw Pin
polycom12313-Jan-11 8:25
polycom12313-Jan-11 8:25 
QuestionHow Do I get IME to open my custom IME UI Windows in C# [modified] Pin
greg ofiesh13-Jan-11 7:48
greg ofiesh13-Jan-11 7:48 
QuestionCalling C++ Image API from C# Pin
econner13-Jan-11 4:59
econner13-Jan-11 4:59 
I am working on implimenting a 3rd party API written in C++. The API pulls an image from a scanner. I have tried a few different ways of of working with the image struct but every attempt causes memory crashes with P/Invoke. Any suggestions would be appreciated.


Here is the C++ code and sample that comes with the device.

//(From header file)
//SCANDLL_API int __stdcall WScanSelectBuf(MY_IMAGE *simage_down,MY_IMAGE *simage_up,int select);


//(structure from C++ documentation
typedef struct {
  int width; // image width
  int height; // image height
  int info; // bit-count (1,4,8 bit)
  unsigned char* pbuf; // image buffer
} MY_IMAGE; // defines the image information



//(Use of method in sample C++ code)

MY_IMAGE img1,img2;

img1.width = 864;
img1.height = 3000;
img1.info = 8; // 8bit
img1.pbuf = (UCHAR *)new UCHAR[2*nWidth*nHeight];

img2.width = 864;
img2.height = 3000;
img2.info = 8; // 8bit
img2.pbuf = (UCHAR *)new UCHAR[2*nWidth*nHeight];

ret=WScanSelectBuf(&img1, &img2,0);

---------------------------------------------------------------------
//C# conversion

[StructLayout(LayoutKind.Explicit)]
    public struct MY_IMAGE
    {
        [FieldOffset(0)]
        public int width;

        [FieldOffset(4)]
        public int height;
        
        [FieldOffset(8)]
        public int info;
        
        //[FieldOffset(12)]
        //public byte[] pbuf; // image buffer as byte array???

        //Stuck here? 
        [FieldOffset(12), MarshalAs(UnmanagedType.SysUInt)]
        public IntPtr pbuf; // image buffer
    }

[DllImport("scandll.dll")]
private extern static int WT_ScanSelectBuf(ref MY_IMAGE image_down, ref MY_IMAGE image_up, int select);


public static int ScanSelectBuf(ref MY_IMAGE image_down, ref MY_IMAGE image_up, int select)
{
  return WScanSelectBuf(ref image_down, ref image_up, select);
}

ImageInfo img1 = new ImageInfo();
ImageInfo img2 = new ImageInfo();


int nWidth = 864;
int nHeight = 1000;

img1.width = nWidth;
img1.height = nHeight;
img1.info = 8; // 8bit
//img1.pbuf = new byte[2 *nWidth * nHeight];
//img1.pbuf = IntPtr.Zero;

img2.width = nWidth;
img2.height = nHeight;
img2.info = 8; // 8bit
//img2.pbuf = new byte[2 * nWidth * nHeight];
//img2.pbuf = IntPtr.Zero;

//CRASHES WITH MEMORY ACCESS/CORRUPT ERRORS
ret = ScanSelectBuf(ref img1,ref img2, 0);

AnswerRe: Calling C++ Image API from C# Pin
_Erik_13-Jan-11 5:16
_Erik_13-Jan-11 5:16 
GeneralRe: Calling C++ Image API from C# Pin
econner13-Jan-11 6:00
econner13-Jan-11 6:00 
GeneralRe: Calling C++ Image API from C# Pin
_Erik_13-Jan-11 6:10
_Erik_13-Jan-11 6:10 
GeneralRe: Calling C++ Image API from C# Pin
econner13-Jan-11 6:16
econner13-Jan-11 6:16 
GeneralRe: Calling C++ Image API from C# Pin
_Erik_13-Jan-11 6:47
_Erik_13-Jan-11 6:47 
GeneralRe: Calling C++ Image API from C# Pin
econner13-Jan-11 6:49
econner13-Jan-11 6:49 
GeneralRe: Calling C++ Image API from C# Pin
_Erik_13-Jan-11 6:55
_Erik_13-Jan-11 6:55 
GeneralRe: Calling C++ Image API from C# Pin
econner13-Jan-11 13:38
econner13-Jan-11 13:38 
GeneralRe: Calling C++ Image API from C# Pin
_Erik_14-Jan-11 1:51
_Erik_14-Jan-11 1:51 
GeneralRe: Calling C++ Image API from C# Pin
econner14-Jan-11 9:12
econner14-Jan-11 9:12 
GeneralRe: Calling C++ Image API from C# Pin
_Erik_14-Jan-11 10:22
_Erik_14-Jan-11 10:22 
GeneralRe: Calling C++ Image API from C# Pin
econner14-Jan-11 11:38
econner14-Jan-11 11:38 
GeneralRe: Calling C++ Image API from C# Pin
econner14-Jan-11 12:07
econner14-Jan-11 12:07 
GeneralRe: Calling C++ Image API from C# Pin
econner14-Jan-11 12:30
econner14-Jan-11 12:30 
GeneralRe: Calling C++ Image API from C# Pin
econner15-Jan-11 5:16
econner15-Jan-11 5:16 
GeneralRe: Calling C++ Image API from C# Pin
_Erik_17-Jan-11 2:32
_Erik_17-Jan-11 2:32 
AnswerRe: Calling C++ Image API from C# Pin
Luc Pattyn13-Jan-11 5:18
sitebuilderLuc Pattyn13-Jan-11 5:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.