473,738 Members | 8,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting SDK C++ function's struct to c#

3 New Member
Hello,

I've a SDK functions description for a scanner and I try to convert unmanaged DLL C++ functions to c#. I converted all except one of them. This function is getting a struct parameter.

Expand|Select|Wrap|Line Numbers
  1. int WINAPI ImgSave(_T_Scan_Param *ScanP, unsigned char *szMicr,
  2.     int *num_chars, int y0, int y1, int cOcrCode_option)
Expand|Select|Wrap|Line Numbers
  1. struct _T_Scan_Param { 
  2.  
  3. char file1[256]; 
  4. char file2[256]; 
  5. char file3[256]; 
  6. unsigned char *imgptr1; 
  7. unsigned char *imgptr2; 
  8. unsigned char *imgptr3; 
  9. int compr_type; 
  10. int compr_quality; 
  11. int imgptr_enable; 
  12. };
My converted c# code is
Expand|Select|Wrap|Line Numbers
  1. private struct Scan_Parameters
  2. {
  3.    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
  4.    public string file1;
  5.    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
  6.    public string file2;
  7.    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
  8.    public string file3;
  9.    public int imgptr1;
  10.    public int imgptr2;
  11.    public int imgptr3;
  12.    public int compr_type;
  13.    public int compr_quality;
  14.    public int imgptr_enable;
  15. }
  16.  
And my declare function is;
Expand|Select|Wrap|Line Numbers
  1. [DllImport(@"c:\scanner.dll")]
  2. private static extern int ImgSave(ref Scan_Parameters scanP, string szMicr, ref int num_chars, int y0, int y1, int cOcrCode_option);
  3.  
When I try, I get an exception like "external component has thrown an exception". I've a " succesfully worked vb6.0 code" for this DLL and in this code, declared function is getting parameters with "ref" so I used these "ref" in my c# code too.
For the other functions, converting from vb6.0 code is working fine but in this function ( I think the reason is about converting struct ) it is not working.

Do u have any idea ? Firstly, is converted struct Ok ?

Thanks
Sep 4 '09 #1
4 3614
Plater
7,872 Recognized Expert Expert
I have seen people have luck by using the StringBuilder for these cases:
unsigned char *imgptr1;
unsigned char *imgptr2;
unsigned char *imgptr3;
(rather then just using an int like you are doing)
Sep 4 '09 #2
screamer81
3 New Member
Thanks for your help Plater.

StringBuilder solved my another problem about a function's unsigned char * parameter; but when I tried to use this in struct, I am getting the exception below:

{"Cannot marshal field 'imgptr1' of type 'Scan_Parameter s': Struct or class fields cannot be of type StringBuilder. The same effect can usually be achieved by using a String field and preinitializing it to a string with length matching the length of the appropriate buffer.":""}

After this exception, I also tried ;

for example : new string(' ', 50 ) but the same exception is occured.

my code is :

private struct Scan_Parameters
{
public string file1;
public string file2;
public string file3;
public StringBuilder imgptr1;
public StringBuilder imgptr2;
public StringBuilder imgptr3;
public int compr_type;
public int compr_quality;
public int imgptr_enable;
}

Scan_Parameters scanParam;

StringBuilder imgarg1 = new StringBuilder(" ", 50);
StringBuilder imgarg2 = new StringBuilder(" ", 50);
StringBuilder imgarg3 = new StringBuilder(" ", 50);

scanParam.file1 = "c:\\images\\im g_fr.jpg";
scanParam.file2 = "c:\\images\\im g_re.jpg";
scanParam.file3 = "";
scanParam.compr _quality = 50;
scanParam.compr _type = 0;
scanParam.imgpt r1 = imgarg1;
scanParam.imgpt r2 = imgarg2;
scanParam.imgpt r3 = imgarg3;
scanParam.imgpt r_enable = 0;

int returnCode = CXL_ScanImgSave (ref scanParam, codeLine, ref codelineLength, 0, 128, 2);


C++ syntax is

unsigned char *imgptr1; // NOT USED
unsigned char *imgptr2; // NOT USED
unsigned char *imgptr3; // NOT USED

"not used" is a commandline but seems it is used somehow.
Sep 4 '09 #3
Plater
7,872 Recognized Expert Expert
Maybe try making them of type IntPtr then?
Sep 8 '09 #4
screamer81
3 New Member
I solved the problem two days ago. Thanks for your help Plater.

c# code is :
Expand|Select|Wrap|Line Numbers
  1. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  2.         private struct Scan_Parameters
  3.         {
  4.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
  5.             public string file1;
  6.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
  7.             public string file2;
  8.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
  9.             public string file3;
  10.             public IntPtr imgptr1;
  11.             public IntPtr imgptr2;
  12.             public IntPtr imgptr3;
  13.             public int compr_type;
  14.             public int compr_quality;
  15.             public int imgptr_enable;
  16.         }
  17.  
Sep 9 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1703
by: Majed | last post by:
hi, I'm trying to convert some .h files to API Declaration to use the function and structs. one of it is this: NTMS_GUID CurrentLibrary; // the current library NTMS_GUID MediaPool; // media pool that the media belongs to NTMS_GUID Location; // actual location of the media
1
1469
by: Thomas Connolly | last post by:
Hello list, No one seems to be replying to my request on the interop group so I thought I would try here. Can someone please help me with this? I have a C struct containing function pointers like so: typedef struct _tagLiffeResponseHandlerList { void (*OnAccessTransfer) (LiffeStatus eStatus, const char* pszTrader, LiffeBoolean bContinuationFlag, int nNumOrders, const LiffeOrderEntryList* pcLiffeOrderEntryList, time_t nTimeStamp);
3
9463
by: VMI | last post by:
How can I convert the following C struct into a C# struct? The struct is sent to an API function (written in C) as parameter, and the function fills it with data. I tried to convert it and most of it works but the data in the foot struct is not correct (I believe it's a conversion problem between C# and C). The class I created in C# (from the struct) is at the bottom. typedef struct { char iadl1;
1
2480
by: VMI | last post by:
How can I convert this struct into a C# struct? The problem with this one is that one of its members is an array of ADDR_REC(another struct), and the API function (written in C) writes to this part of the struct. I call the function with struct CITY_REC as parameter and the API function writes to this stack. Any help is appreciated. I've converted ADDR_REC to C# but I have no idea what to do with CITY_REC. typedef struct { char ...
3
2648
by: Thomas Connolly | last post by:
Hello list, No one seems to be replying to my request on the interop group so I thought I would try here. Can someone please help me with this? I have a C struct containing function pointers like so: typedef struct _tagLiffeResponseHandlerList { void (*OnAccessTransfer) (LiffeStatus eStatus, const char* pszTrader, LiffeBoolean bContinuationFlag, int nNumOrders, const LiffeOrderEntryList* pcLiffeOrderEntryList, time_t nTimeStamp);
4
2086
by: Brett | last post by:
Hi, I'm having trouble converting an old project to visual studio C++ .net v7.1.3088 I've updated one of the header files to use #include <iostream>, and used the std namspace. After resolving the missing library by adding "-NODEFAULTLIB:msvcirt.lib" to the linker command line, I get the following link errors:
7
3850
by: The Cool Giraffe | last post by:
I have created the following conversion in the header file for my struct called SomeStruct. operator std::string () {return "some useless text";} I'm using it as follows. SomeStruct someS; std:cout << (std::string)someS;
4
1526
by: Studlyami | last post by:
I know that title isn't a great description so I apologize , but i don't know how to summaries it (probably also why i can't find any information online). I have a struct that contains 3 ints (used for coordinates) and i want a function to be able to pass in 3 ints and have the struct convert the 3 ints into a struct object. struct VectorCords { int x, y, z; }; DoSomething (VectorCords VectorInfo) //Function that accepts...
9
5171
by: ssubbarayan | last post by:
Hi all, I am trying a program to convert floating point values to a byte array and printing the same to the screen.The idea behind this is we already have an existing function which can do byte level parsing what ever may be the type of data.The data would be coming from an external environment.When I parse int,char at byte level,I get right values,where as floating point just prints 0.000000 to the screen.Given below is a sample program...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.