Solved

C++ memset to C# AllocHGlobal ( ? )

Posted on 2004-03-23
6
2,303 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
Technology Partners: 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!

 
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 500 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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

733 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