Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi.

I need help with my code.
I'm trying to run unmanaged code from my C# code.


C++ Code
C++
Result_t oemGetImage(PBYTE pImageBuffer,
                     PDWORD pdwSize, WORD nTop,
                     WORD nLeft, WORD nRight,
                     WORD nBottom, WORD nSkip,
                     WORD nBits, FileFormat_t nFormat,
                     WORD nWhiteValue,
                     WORD nExposeAttempts, WORD nGap,
                     BOOL Invert, void (*fpProgress) (WORD));


I don't know what to do with pImageBuffer, pdwSize or void (*fpProgress) (WORD)).

In C++ I execute this function like this:
C++
BYTE *pBufor;
pBufor = new BYTE[MAX_IMAGE_SIZE];

DWORD *pSize; 
	pSize = new DWORD; 

oemGetImage (pBufor,pSize, 0, 0, 200, 200, 1, 8, FF_RAW_GRAY, 200, 100, 55, false, NULL);

FF_RAW_GRAY is an enum with value of 1.

How can I declare pBufor which is a pointer to byte array?
And what to do with void (*fpProgress) (WORD))??

Thank you, for all your help.
Posted

Generated by P/Invoke interop assistant

VB
public partial class NativeMethods {

    
    [System.Runtime.InteropServices.DllImportAttribute("<PATH TO YOUR DLL >", EntryPoint="oemGetImage")]
public static extern  System.IntPtr oemGetImage(ref byte[] pImageBuffer, ref uint pdwSize, ushort nTop, ushort nLeft, ushort nRight, ushort nBottom, ushort nSkip, ushort nBits, System.IntPtr nFormat, ushort nWhiteValue, ushort nExposeAttempts, ushort nGap, [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] bool Invert, System.IntPtr param13) ;

}


void (*fpProgress) (WORD)) it's a delegate to method -> do nothing with it(use IntPtr.Zero)

return type and nFormat(not shure about that type, check the declaration in c++ code) is unknown so it used like pointer to some unknown object type (use IntPtr)


try to use that way

byte[] pImageBuffer = new byte[MAX_IMAGE_SIZE];
uint pdwSize;

var nFormat =  check the type

IntPtr result =  NativeMethods.oemGetImage(ref  pImageBuffer[], ref pdwSize, 0, 0, 200, 200, 1, 8, nFormat,  200, 100, 55, false, IntPtr.Zero);


Try to move alone from here
I was glad to help you , sorry for my English.
Roman
 
Share this answer
 
v3
Comments
GigaKatowice 21-Jun-12 5:49am    
Unfortunately yous solution didn't work. I get an exception, but luckily with help from other forum I managed to run my code. I posted this solution below.
Thanks for your help.
If you have access to the C++ code and can easily change it, then the recommended way would be to declare exported functions using Automation Types only.

For instance, to pass an array of values, use type LPSAFEARRAY as the parameter's type in C++.

Automation Types like this can map into .NET types easily, without much overhead.

For example, to map LPSAFEARRAY that represents a pointer to an array of bytes that you want to pass from C++ and into .NET client, you would use the following parameter declaration in .NET:

[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_UI1)] out byte[] myArray


And you wouldn't need to pass the size of the array in this case, as the array contains information about its size.
 
Share this answer
 
v2
Thank you for help.

I found a solution for my problem on c-sharpcorner.

Declaration:
C#
 private extern static int oemGetImage([In, Out]byte[] pImageBuffer,
         ref uint pdwSize, uint nTop,
         uint nLeft, uint nRight,
         uint nBottom, uint nSkip,
         uint nBits, uint nFormat,
         uint nWhiteValue,
         uint nExposeAttempts, uint nGap,
         bool Invert, [MarshalAs(UnmanagedType.FunctionPtr)]Progress fpProgress);

public delegate void Progress(ushort us);



Execution:
C#
byte[] pBufor = new byte[500000];
uint pSize = 0;

i = oemGetImage(pBufor, ref pSize, 0, 0, _width, _height, 1, 8, 1, 220, 50, 35, false, null);


I think that main problem was with the last argument of this function.
However
C#
ref byte[] pImageBuffer
didn't work too.

I tried many combinations and only that one above worked fine.
Anyway thank you for help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900