Click here to Skip to main content
15,897,371 members
Home / Discussions / C#
   

C#

 
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 
Yes, you are pinning the MY_IMAGE objects, but that is not what you have to pin. When you call a function using P/Invoke, the CLR automatically pinnes in the managed heap the references you pass, so there is no need to explicitly pin them as you have done. However, the pbuf field of each MY_IMAGE class is an IntPtr, what means that it only contains the memory address of the buffer allocated to receive de image pixels. As it is only an address, the CLR will not pin these buffers automatically, and that is why the programmer has to pin them manually before using the P/Invoke call. In this case you have not even allocated the buffers, so the values of the pbuf fields of the MY_IMAGE objects you are passing to the unmanaged function are IntPtr.Zero, I mean, the default value for IntPtr.

This is what you have to do:

Before you call the WScanSelectBuf you have to create the two MY_IMAGE objects and allocate the byte[] arrays where that function will place the image pixel:

C#
MY_IMAGE img1 = new MY_IMAGE();
MY_IMAGE img2 = new MY_IMAGE();

// Initialize the other fields, I mean, width, height and info
// Then create the byte[] arrays
// with the size given by the documentation
byte[] buffer1 = new byte[2*img1.width*img1.height];
byte[] buffer2 = new byte{2*img2.width*img2.height];

// Pin these two arrays in the managed heap
GCHandle bufferHandle1 = GCHandle.Alloc(buffer1, GCHandleType.Pinned);
GCHandle bufferHandle2 = GCHandle.Alloc(buffer2, GCHandleType.Pinned);

// Set the address of the buffers to each pbuf field
img1.pbuf = bufferHandle1.AddrOfPinnedObject();
img2.pbuf = bufferHandle2.AddrOfPinnedObject();

// And now we can make the P/Invoke call
int ret = WScanSelectBuf(img1, img2, select);

// Do not forget to free the handles
bufferHandle1.Free();
bufferHandle2.Free();

// Here you should have the pixel information in the buffer1 and buffer2 arrays


Tell us if it works.
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 
AnswerRe: Calling C++ Image API from C# [modified] Pin
econner13-Jan-11 6:11
econner13-Jan-11 6:11 
AnswerRe: Calling C++ Image API from C# Pin
Luc Pattyn13-Jan-11 6:22
sitebuilderLuc Pattyn13-Jan-11 6:22 
GeneralRe: Calling C++ Image API from C# Pin
econner13-Jan-11 6:49
econner13-Jan-11 6:49 
AnswerRe: Calling C++ Image API from C# Pin
Luc Pattyn13-Jan-11 6:56
sitebuilderLuc Pattyn13-Jan-11 6:56 
GeneralRe: Calling C++ Image API from C# Pin
econner13-Jan-11 7:00
econner13-Jan-11 7:00 
AnswerRe: Calling C++ Image API from C# Pin
jschell13-Jan-11 8:06
jschell13-Jan-11 8:06 
QuestionHow to record audio and modify the signal? Pin
softwarejaeger13-Jan-11 3:41
softwarejaeger13-Jan-11 3:41 
AnswerRe: How to record audio and modify the signal? Pin
Rob Philpott13-Jan-11 4:17
Rob Philpott13-Jan-11 4:17 
AnswerRe: How to record audio and modify the signal? Pin
Luc Pattyn13-Jan-11 4:28
sitebuilderLuc Pattyn13-Jan-11 4:28 

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.