?
Solved

C++ memset to C# AllocHGlobal ( ? )

Posted on 2004-03-23
6
Medium Priority
?
2,338 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

765 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