Solved

C++ memset to C# AllocHGlobal ( ? )

Posted on 2004-03-23
6
2,271 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

23 Experts available now in Live!

Get 1:1 Help Now