Solved

C++ memset to C# AllocHGlobal ( ? )

Posted on 2004-03-23
6
2,265 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now