473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Unmanaged pointer

Hello... I am making a Sytem.IntPtr with Marshal.AllocHGlobal(int)
(Allocates memory from the unmanaged memory of the process using
GlobalAlloc - MSDN Definition for Marshal.AllocHGlobal) and after
copying the data from a byte[] buffer to the IntPtr(with
Marshal.Copy), I send the IntPtr to an unmanaged library. In the
unmanaged library the function's signature has a unsigned char *
parameter(my IntPtr from C#) and in function's code-block I want to
get the pointer's value.

I use this code to put the pointer's value into a txt file

//C++ code
char* b = new char[10];
sprintf(b,"%2d", (int)pMyPointer);
ofstream out((char *)"aaa.txt");
out.write((const char*)b, 10);
delete[] b;

In C#, when debugging, I have a value for my System.IntPtr but when I
open the txt file, I see other value.

Why is that? Isn't Marshal.AllocHGlobal return pointer a pointer on
the unmanaged heap to an unmanaged memory?
Thanks

Jul 23 '07 #1
2 3327
It seems like you are just trying to pass a string to your function.
Why not just use a String parameter and declare the function with the
MarshalAs attribute with a value of UnmanagedType.LPStr?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"eusebiu" <Ma**********@gmail.comwrote in message
news:11**********************@n60g2000hse.googlegr oups.com...
Hello... I am making a Sytem.IntPtr with Marshal.AllocHGlobal(int)
(Allocates memory from the unmanaged memory of the process using
GlobalAlloc - MSDN Definition for Marshal.AllocHGlobal) and after
copying the data from a byte[] buffer to the IntPtr(with
Marshal.Copy), I send the IntPtr to an unmanaged library. In the
unmanaged library the function's signature has a unsigned char *
parameter(my IntPtr from C#) and in function's code-block I want to
get the pointer's value.

I use this code to put the pointer's value into a txt file

//C++ code
char* b = new char[10];
sprintf(b,"%2d", (int)pMyPointer);
ofstream out((char *)"aaa.txt");
out.write((const char*)b, 10);
delete[] b;

In C#, when debugging, I have a value for my System.IntPtr but when I
open the txt file, I see other value.

Why is that? Isn't Marshal.AllocHGlobal return pointer a pointer on
the unmanaged heap to an unmanaged memory?
Thanks

Jul 23 '07 #2

"eusebiu" <Ma**********@gmail.comwrote in message
news:11**********************@n60g2000hse.googlegr oups.com...
Hello... I am making a Sytem.IntPtr with Marshal.AllocHGlobal(int)
(Allocates memory from the unmanaged memory of the process using
GlobalAlloc - MSDN Definition for Marshal.AllocHGlobal) and after
copying the data from a byte[] buffer to the IntPtr(with
Marshal.Copy), I send the IntPtr to an unmanaged library. In the
unmanaged library the function's signature has a unsigned char *
parameter(my IntPtr from C#) and in function's code-block I want to
get the pointer's value.

I use this code to put the pointer's value into a txt file

//C++ code
char* b = new char[10];
sprintf(b,"%2d", (int)pMyPointer);
ofstream out((char *)"aaa.txt");
out.write((const char*)b, 10);
delete[] b;
I think that ought to work, but really, that's some atrocious code. Try
instead:

ofstream out("aaa.txt");
out << hex << pMyPointer << flush;
>
In C#, when debugging, I have a value for my System.IntPtr but when I
open the txt file, I see other value.
Maybe two different runs of the program? The pointer isn't guaranteed to be
the same every time.

Anyway, is there are reason you aren't using a pinned byte*?
Jul 23 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Lev | last post by:
Hi, I have an unmanaged pointer to a class that I want to hold in a managed class. I pass the pointer (from unmanaged code) in the constructor of the managed class, and at that point it has...
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
2
by: The unProfessional | last post by:
In my current project (my first project using vc w/ managed extensions), I'm directly #using <mscorlib.dll>, so it's necessary for me to use the __nogc and __gc constructs when defining classes or...
1
by: Sparhawk | last post by:
Hi, my company is going to migrate a large VC++ application to .NET to make use of Windows Forms (the old class library is not updated any more). We are not planning to migrate the rest of the...
4
by: Maxwell | last post by:
Hello, Newbie question here for disposing of unmanaged resources in MC++.NET. I have a managed VS.NET 2003 MC++ wrapper class that wraps a unmanaged C++ dll. What I am trying to figure out is...
5
by: R. MacDonald | last post by:
Hello, all, I am currently working on a .Net (VB) application that invokes routines in unmanaged (Fortran) DLLs. The unmanaged routines then communicate with the .Net application by means of a...
6
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example...
12
by: DaTurk | last post by:
Hi, I have a rather interesting problem. I have a unmanged c++ class which needs to communicate information to managed c++ via callbacks, with a layer of c# on top of the managed c++ ultimatley...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.