Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++ memset to C# AllocHGlobal ( ? )

Posted on 2004-03-23
6
Medium Priority
?
2,360 Views
Last Modified: 2007-12-19
I've got this code that exists as such ..

memset(&JobInfo, 0, sizeof(JobInfo));

I believe this declares the reference to JobInfo ( which is a type of TOCRJOBINFO structure, defined below ) to the right memory size. I need to figure out the equivalent to this in C#.

typedef struct tagTOCRJobInfo
{
      long            StructId;
      long            JobType;
      char            *InputFile;
      long            PageNo;
      TOCRPROCESSOPTIONS      ProcessOptions;
} TOCRJOBINFO;

My C# Declaration of the TOCRJobInfo structure is as follows...

      [StructLayout(LayoutKind.Sequential)]
      public struct TOCRJobInfo
      {
            public int                              StructId;
            public int                              JobType;
            public String                        InputFile;
            public int                              PageNo;
            public TOCRProcessOptions      ProcessOptions;
      }


Any suggestions?
0
Comment
Question by:derekthornton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10658142
memset(&JobInfo, 0, sizeof(JobInfo));

This line sets 0 to all bytes of JobInfo structure.
0
 
LVL 2

Author Comment

by:derekthornton
ID: 10658201
memset isn't available in C# though .. How can I achieve the same result in C#? AllocHGlobal doesn't seem to do what I thought it did ( I thought it would be the way to go, but it doesn't seem that way )
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10658211
Except of this, I think mapping of
char          *InputFile;
to
public String                    InputFile;
is wrong. char* should be mapped to IntPtr. To set it's value use Marshal.StringToCoTaskMemAnsi Method and set value returned by it to InputFile.

I don't know how exactly to reproduce memset in .NET using one function call, but setting 0 to each structure member (including members of internal structure) makes the sane effect.


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 48

Expert Comment

by:AlexFM
ID: 10658282
Take a look at this article:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09192002.asp

See Simple Strings part. LPCTSTR is mapped there to
[MarshalAs(UnmanagedType.LPTStr)]
   string ...

However, your structure has char* and not LPCTSTR, so try UnmanagedType.LPStr instead of UnmanagedType.LPTStr.

I beleive my way using IntPtr and StringToCoTaskMemAnsi is OK also, but it's better to follow this article sample.
0
 
LVL 2

Author Comment

by:derekthornton
ID: 10658291
You can't use Pointers in safe context in C#.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 2000 total points
ID: 10658455
I don't see full C++ code, so I just guess.

C++

typedef struct tagSampleStructure
{
     long          n1;
     long          n2;
     char          *InputFile;
} SAMPLESTRUCTURE;

SAMPLESTRUCTURE SampleStructure;

memset((&SampleStructure, 0, sizeof(SampleStructure));
SampleStructure.InputFile = "file1.txt";

SomeFunction(&SampleStructure);

C#:

[StructLayout(LayoutKind.Sequential)]
public struct SAMPLESTRUCTURE
{
    public int n1;
    public int n2;
    [MarshalAs(UnmanagedType.LPStr)]
    public string InputFile;
};

SAMPLESTRUCTURE SampleStructure;
SampleStructure.n1 = 0;
SampleStructure.n2 = 0;
SampleStructure..InputFile = "file1.txt";

SomeFunction(SampleStructure);
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question