473,385 Members | 1,326 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,385 software developers and data experts.

Help please with pointers to callbacks in structs

I am very new to C# programming and have run into a problem.

I apologize for the repost of this article. For some reason, my news
reader attached it to an existing thread.

First off, I have an SDK that I have written for C/C++ and would like to
port it to C# if at all possible.

Basically, I've got a structure that I need to pass to the C-style DLL
(i.e. no mangled names - everything is __stdcall). This structure needs
to contain pointers to callback functions that the DLL can use to notify
the C# application when certain events occur. In addition, I need to use
SetEnvironmentVariable and pass a string containing a pointer to a
character array for special processing by the DLL. I've included some
code that shows basically everything I'm looking to do.

To save time, I understand how to get access to exported functions within
DLLs.
// this structure will be filled by the DLL and will be passed
// to the callback for function2, (i.e. FnPtr2's typedef)

typedef struct _toPassAsArgumentStruct
{
int a;
int b;
double c;
} TO_PASS_AS_ARGUMENT;
// these are the typedef's for two callback functions that will be
// stored in the structure, DEMOSTRUCT

typedef BOOL (__stdcall *FnPtr1)(DWORD i, char *path, DWORD pathLength);
typedef BOOL (__stdcall *FnPtr2)(TO_PASS_AS_ARGUMENT *structure);
typedef struct _demoStruct
{
FnPtr1 function1;
FnPtr2 function2;
} DEMOSTRUCT;
char global_text[5] = {"test"};
// MyFunction1 and MyFunction2 are called by the DLL

BOOL MyFunction1(DWORD i, char *path, DWORD pathLength)
{
strcpy(path, "unknown");
return TRUE;
}

BOOL MyFunction2(TO_PASS_AS_ARGUMENT *structure)
{
structure->a = 5;
return TRUE;
}
// simple_function is called by the non-DLL (i.e. the application)

void simple_function()
{
char text[128] = {0};

DEMOSTRUCT ds = {0};

// Are these four statements possible in C#?

ds.function1 = MyFunction1;
ds.function2 = MyFunction2;

// the contents of 'text' will be passed to the DLL.
sprintf(text, "%x", &global_text);

SetEnvironmentVariable("test", text);
}
--

TIA,
Brian
Nov 16 '05 #1
23 2202
Hi,
see inline;

<Brian> wrote in message news:10*************@corp.supernews.com...
I am very new to C# programming and have run into a problem.

I apologize for the repost of this article. For some reason, my news
reader attached it to an existing thread.

First off, I have an SDK that I have written for C/C++ and would like to
port it to C# if at all possible.

Basically, I've got a structure that I need to pass to the C-style DLL
(i.e. no mangled names - everything is __stdcall). This structure needs
to contain pointers to callback functions that the DLL can use to notify
the C# application when certain events occur. In addition, I need to use
SetEnvironmentVariable and pass a string containing a pointer to a
character array for special processing by the DLL. I've included some
code that shows basically everything I'm looking to do.

To save time, I understand how to get access to exported functions within
DLLs.
// this structure will be filled by the DLL and will be passed
// to the callback for function2, (i.e. FnPtr2's typedef)

typedef struct _toPassAsArgumentStruct
{
int a;
int b;
double c;
} TO_PASS_AS_ARGUMENT;
In c#:

[StructLayout(StructLayout.Sequential)]
public struct PassAsArgStruct
{
public int a;
public int b;
public double c;
}


// these are the typedef's for two callback functions that will be
// stored in the structure, DEMOSTRUCT

typedef BOOL (__stdcall *FnPtr1)(DWORD i, char *path, DWORD pathLength);
typedef BOOL (__stdcall *FnPtr2)(TO_PASS_AS_ARGUMENT *structure);
Declare a delegate for each callback:
public delegate bool DelFn1(uint i, IntPtr pPath, uint pathLength);
public delegate bool DelFn2(ref ToPassAsArgStruct structure);


typedef struct _demoStruct
{
FnPtr1 function1;
FnPtr2 function2;
} DEMOSTRUCT;
In c#: (use the delegates)

[StructLayout(StructLayout.Sequential)]
public struct DemoStruct
{
public DelFn1 function1;
public DelFn2 function2;
}


// MyFunction1 and MyFunction2 are called by the DLL

BOOL MyFunction1(DWORD i, char *path, DWORD pathLength)
{
strcpy(path, "unknown");
return TRUE;
}
[DllImport("kernel32.dll",CharSet=CharSet.Ansi)]
public static extern long lstrcpyA (IntPtr pDst, string sSrc);

private bool MyFunction1(uint i, IntPtr pPath, uint pathLength)
{
// TODO check pathLength before writing
lstrcpyA (pPath, "unknown");
return true;
}

BOOL MyFunction2(TO_PASS_AS_ARGUMENT *structure)
{
structure->a = 5;
return TRUE;
}
In c#:

private bool MyFunction2(ref ToPassAsArgStruct s)
{
s.a = 5;
return true;
}
char global_text[5] = {"test"};

// simple_function is called by the non-DLL (i.e. the application)

void simple_function()
{
char text[128] = {0};

DEMOSTRUCT ds = {0};

// Are these four statements possible in C#?

ds.function1 = MyFunction1;
ds.function2 = MyFunction2;

// the contents of 'text' will be passed to the DLL.
sprintf(text, "%x", &global_text);

SetEnvironmentVariable("test", text);
[DllImport("kernel32.dll",CharSet=CharSet.Auto)]
public static extern bool SetEnvironmentVariable (string sName, string
sValue);

private DemoStruct ds;
private IntPtr pGlobalText = IntPtr.Zero;

void SimpleFunction()
{
ds = new DemoStruct();
ds.function1 = new DelFn1( MyFunction1 );
ds.function2 = new DelFn2( MyFunction2 );

// Allocate unmanaged memory the dll can read/write
pGlobalText = Marshal.AllocHGlobal (5);

lstrcpyA ( pGlobalText, "test" ); // write

// pass pointer as hex string value
SetEnvironmentVariable("test", pGlobalText.ToInt32().ToString("X") );
}

* After this function you can :
- read from globaltext
Console.WriteLine ( Marshal.PtrToStringAnsi( pGlobalText ) ); /// READ

- write to globaltext
lstrcpyA ( pGlobalText, text );

- cleanup globaltext
(only if dll doesn't need globaltext anymore)
Marshal.FreeHGlobal( pGlobalText );
HTH,
greetings


}
--

TIA,
Brian

Nov 16 '05 #2
Thank you so much! I REALLY appreciate your taking the time to step me
through this!

I'll give it a try and see what happens.

Brian

BMermuys <so*****@someone.com> wrote:
Hi,
see inline; <Brian> wrote in message news:10*************@corp.supernews.com...
I am very new to C# programming and have run into a problem.

I apologize for the repost of this article. For some reason, my news
reader attached it to an existing thread.

First off, I have an SDK that I have written for C/C++ and would like to
port it to C# if at all possible.

Basically, I've got a structure that I need to pass to the C-style DLL
(i.e. no mangled names - everything is __stdcall). This structure needs
to contain pointers to callback functions that the DLL can use to notify
the C# application when certain events occur. In addition, I need to use
SetEnvironmentVariable and pass a string containing a pointer to a
character array for special processing by the DLL. I've included some
code that shows basically everything I'm looking to do.

To save time, I understand how to get access to exported functions within
DLLs.
// this structure will be filled by the DLL and will be passed
// to the callback for function2, (i.e. FnPtr2's typedef)

typedef struct _toPassAsArgumentStruct
{
int a;
int b;
double c;
} TO_PASS_AS_ARGUMENT;
In c#: [StructLayout(StructLayout.Sequential)]
public struct PassAsArgStruct
{
public int a;
public int b;
public double c;
}

// these are the typedef's for two callback functions that will be
// stored in the structure, DEMOSTRUCT

typedef BOOL (__stdcall *FnPtr1)(DWORD i, char *path, DWORD pathLength);
typedef BOOL (__stdcall *FnPtr2)(TO_PASS_AS_ARGUMENT *structure); Declare a delegate for each callback:
public delegate bool DelFn1(uint i, IntPtr pPath, uint pathLength);
public delegate bool DelFn2(ref ToPassAsArgStruct structure);

typedef struct _demoStruct
{
FnPtr1 function1;
FnPtr2 function2;
} DEMOSTRUCT; In c#: (use the delegates) [StructLayout(StructLayout.Sequential)]
public struct DemoStruct
{
public DelFn1 function1;
public DelFn2 function2;
}

// MyFunction1 and MyFunction2 are called by the DLL

BOOL MyFunction1(DWORD i, char *path, DWORD pathLength)
{
strcpy(path, "unknown");
return TRUE;
} [DllImport("kernel32.dll",CharSet=CharSet.Ansi)]
public static extern long lstrcpyA (IntPtr pDst, string sSrc); private bool MyFunction1(uint i, IntPtr pPath, uint pathLength)
{
// TODO check pathLength before writing
lstrcpyA (pPath, "unknown");
return true;
}
BOOL MyFunction2(TO_PASS_AS_ARGUMENT *structure)
{
structure->a = 5;
return TRUE;
}

In c#:

private bool MyFunction2(ref ToPassAsArgStruct s)
{
s.a = 5;
return true;
}
char global_text[5] = {"test"};

// simple_function is called by the non-DLL (i.e. the application)

void simple_function()
{
char text[128] = {0};

DEMOSTRUCT ds = {0};

// Are these four statements possible in C#?

ds.function1 = MyFunction1;
ds.function2 = MyFunction2;

// the contents of 'text' will be passed to the DLL.
sprintf(text, "%x", &global_text);

SetEnvironmentVariable("test", text); [DllImport("kernel32.dll",CharSet=CharSet.Auto)]
public static extern bool SetEnvironmentVariable (string sName, string
sValue); private DemoStruct ds;
private IntPtr pGlobalText = IntPtr.Zero; void SimpleFunction()
{
ds = new DemoStruct();
ds.function1 = new DelFn1( MyFunction1 );
ds.function2 = new DelFn2( MyFunction2 ); // Allocate unmanaged memory the dll can read/write
pGlobalText = Marshal.AllocHGlobal (5); lstrcpyA ( pGlobalText, "test" ); // write // pass pointer as hex string value
SetEnvironmentVariable("test", pGlobalText.ToInt32().ToString("X") );
} * After this function you can :
- read from globaltext
Console.WriteLine ( Marshal.PtrToStringAnsi( pGlobalText ) ); /// READ - write to globaltext
lstrcpyA ( pGlobalText, text ); - cleanup globaltext
(only if dll doesn't need globaltext anymore)
Marshal.FreeHGlobal( pGlobalText );
HTH,
greetings



}
--

TIA,
Brian

Nov 16 '05 #3
Hi,

I failed to mention that I'll be using an exported function within a
custom DLL that I'll need to pass the following structure to. Will there
be any problems with the garbage collection freeing up the pointers to my
callbacks? I need the callbacks to be available throughout the life-cycle
of the program. The structure itself can be discarded as my DLL simply
copies the contents into its own format.

basically...

// dll function prototype

void __stdcall AssignCallbacks(DemoStruct *pDemoStruct);

I'm guessing the C# code would be...

[DllImport("mydll.dll",CharSet=CharSet.Ansi)]
public static extern void AssignCallbacks(ref DemoStruct pDemoStruct);

.... and I would call it like so ...

void SimpleFunction()
{
// DemoStruct as is defined below...

ds = new DemoStruct();
ds.function1 = new DelFn1( MyFunction1 );
ds.function2 = new DelFn2( MyFunction2 );

AssignCallbacks(ds);
}

Thanks,
Brian

BMermuys <so*****@someone.com> wrote:
[StructLayout(StructLayout.Sequential)]
public struct DemoStruct
{
public DelFn1 function1;
public DelFn2 function2;
} [DllImport("kernel32.dll",CharSet=CharSet.Ansi)]
public static extern long lstrcpyA (IntPtr pDst, string sSrc); private bool MyFunction1(uint i, IntPtr pPath, uint pathLength)
{
// TODO check pathLength before writing
lstrcpyA (pPath, "unknown");
return true;
}

private bool MyFunction2(ref ToPassAsArgStruct s)
{
s.a = 5;
return true;
}

[DllImport("kernel32.dll",CharSet=CharSet.Auto)]
public static extern bool SetEnvironmentVariable (string sName, string
sValue);

private DemoStruct ds;
private IntPtr pGlobalText = IntPtr.Zero;

void SimpleFunction()
{
ds = new DemoStruct();
ds.function1 = new DelFn1( MyFunction1 );
ds.function2 = new DelFn2( MyFunction2 );

// Allocate unmanaged memory the dll can read/write
pGlobalText = Marshal.AllocHGlobal (5);

lstrcpyA ( pGlobalText, "test" ); // write

// pass pointer as hex string value
SetEnvironmentVariable("test", pGlobalText.ToInt32().ToString("X") );
}

* After this function you can :
- read from globaltext
Console.WriteLine ( Marshal.PtrToStringAnsi( pGlobalText ) ); /// READ - write to globaltext
lstrcpyA ( pGlobalText, text ); - cleanup globaltext
(only if dll doesn't need globaltext anymore)
Marshal.FreeHGlobal( pGlobalText );
HTH,


Believe me, it did. Thanks!
Brian
Nov 16 '05 #4
Hi,

<Brian> wrote in message news:10*************@corp.supernews.com...
Hi,

I failed to mention that I'll be using an exported function within a
custom DLL that I'll need to pass the following structure to. Will there
be any problems with the garbage collection freeing up the pointers to my
callbacks?
Yes if you don't keep a reference to the struct.

Make sure you can access the DemoStruct variable during the entire program
(this is the same as saying you should keep a reference somewhere). Then
the GC won't cleanup the delegates. As long as the delegates are alive and
not changed the function pointers remain valid.

Eg. declare demostruct as a instance variable or static variable in a class
(*not* as a local variable inside a function ).
I need the callbacks to be available throughout the life-cycle
of the program. The structure itself can be discarded as my DLL simply
Yes, your dll may copy the struct. But you must keep the managed struct
after 'assigncallbacks'.
copies the contents into its own format.

basically...

// dll function prototype

void __stdcall AssignCallbacks(DemoStruct *pDemoStruct);

I'm guessing the C# code would be...

[DllImport("mydll.dll",CharSet=CharSet.Ansi)]
public static extern void AssignCallbacks(ref DemoStruct pDemoStruct);
Yes that's it.

Don't hesitate to ask more if required.

hth,
greetings


... and I would call it like so ...

void SimpleFunction()
{
// DemoStruct as is defined below...

ds = new DemoStruct();
ds.function1 = new DelFn1( MyFunction1 );
ds.function2 = new DelFn2( MyFunction2 );

AssignCallbacks(ds);
}

Thanks,
Brian

BMermuys <so*****@someone.com> wrote:
[StructLayout(StructLayout.Sequential)]
public struct DemoStruct
{
public DelFn1 function1;
public DelFn2 function2;
}

[DllImport("kernel32.dll",CharSet=CharSet.Ansi)]
public static extern long lstrcpyA (IntPtr pDst, string sSrc);

private bool MyFunction1(uint i, IntPtr pPath, uint pathLength)
{
// TODO check pathLength before writing
lstrcpyA (pPath, "unknown");
return true;
}

private bool MyFunction2(ref ToPassAsArgStruct s)
{
s.a = 5;
return true;
}

[DllImport("kernel32.dll",CharSet=CharSet.Auto)]
public static extern bool SetEnvironmentVariable (string sName, string
sValue);

private DemoStruct ds;
private IntPtr pGlobalText = IntPtr.Zero;

void SimpleFunction()
{
ds = new DemoStruct();
ds.function1 = new DelFn1( MyFunction1 );
ds.function2 = new DelFn2( MyFunction2 );

// Allocate unmanaged memory the dll can read/write
pGlobalText = Marshal.AllocHGlobal (5);

lstrcpyA ( pGlobalText, "test" ); // write

// pass pointer as hex string value
SetEnvironmentVariable("test", pGlobalText.ToInt32().ToString("X") ); }

* After this function you can :
- read from globaltext
Console.WriteLine ( Marshal.PtrToStringAnsi( pGlobalText ) ); /// READ

- write to globaltext
lstrcpyA ( pGlobalText, text );

- cleanup globaltext
(only if dll doesn't need globaltext anymore)
Marshal.FreeHGlobal( pGlobalText );


HTH,


Believe me, it did. Thanks!
Brian

Nov 16 '05 #5
Thanks for the help. It is very much appreciated.

I figured that the GC might behave the way that you described, but keeping
an active reference shouldn't be too difficult.

Thanks,
Brian
Hi, <Brian> wrote in message news:10*************@corp.supernews.com...
Hi,

I failed to mention that I'll be using an exported function within a
custom DLL that I'll need to pass the following structure to. Will there
be any problems with the garbage collection freeing up the pointers to my
callbacks?
Yes if you don't keep a reference to the struct. Make sure you can access the DemoStruct variable during the entire program
(this is the same as saying you should keep a reference somewhere). Then
the GC won't cleanup the delegates. As long as the delegates are alive and
not changed the function pointers remain valid. Eg. declare demostruct as a instance variable or static variable in a class
(*not* as a local variable inside a function ).
I need the callbacks to be available throughout the life-cycle
of the program. The structure itself can be discarded as my DLL simply Yes, your dll may copy the struct. But you must keep the managed struct
after 'assigncallbacks'. copies the contents into its own format.

basically...

// dll function prototype

void __stdcall AssignCallbacks(DemoStruct *pDemoStruct);

I'm guessing the C# code would be...

[DllImport("mydll.dll",CharSet=CharSet.Ansi)]
public static extern void AssignCallbacks(ref DemoStruct pDemoStruct); Yes that's it. Don't hesitate to ask more if required. hth,
greetings



... and I would call it like so ...

void SimpleFunction()
{
// DemoStruct as is defined below...

ds = new DemoStruct();
ds.function1 = new DelFn1( MyFunction1 );
ds.function2 = new DelFn2( MyFunction2 );

AssignCallbacks(ds);
}

Thanks,
Brian

BMermuys <so*****@someone.com> wrote:
> [StructLayout(StructLayout.Sequential)]
> public struct DemoStruct
> {
> public DelFn1 function1;
> public DelFn2 function2;
> }

> [DllImport("kernel32.dll",CharSet=CharSet.Ansi)]
> public static extern long lstrcpyA (IntPtr pDst, string sSrc);

> private bool MyFunction1(uint i, IntPtr pPath, uint pathLength)
> {
> // TODO check pathLength before writing
> lstrcpyA (pPath, "unknown");
> return true;
> }
>
> private bool MyFunction2(ref ToPassAsArgStruct s)
> {
> s.a = 5;
> return true;
> }
>
> [DllImport("kernel32.dll",CharSet=CharSet.Auto)]
> public static extern bool SetEnvironmentVariable (string sName, string
> sValue);
>
> private DemoStruct ds;
> private IntPtr pGlobalText = IntPtr.Zero;
>
> void SimpleFunction()
> {
> ds = new DemoStruct();
> ds.function1 = new DelFn1( MyFunction1 );
> ds.function2 = new DelFn2( MyFunction2 );
>
> // Allocate unmanaged memory the dll can read/write
> pGlobalText = Marshal.AllocHGlobal (5);
>
> lstrcpyA ( pGlobalText, "test" ); // write
>
> // pass pointer as hex string value
> SetEnvironmentVariable("test",

pGlobalText.ToInt32().ToString("X") ); > }
>
> * After this function you can :
> - read from globaltext
> Console.WriteLine ( Marshal.PtrToStringAnsi( pGlobalText ) ); /// READ

> - write to globaltext
> lstrcpyA ( pGlobalText, text );

> - cleanup globaltext
> (only if dll doesn't need globaltext anymore)
> Marshal.FreeHGlobal( pGlobalText );


> HTH,


Believe me, it did. Thanks!
Brian


--
Brian
Nov 16 '05 #6
I've got two more questions, and *hopefully* that'll be all for a while.

First, I've got a character array (either signed or unsigned depending
on the usage) that will be passed to a callback. To complicate matters,
this character array is modifiable. I'm assuming that I should use define
my callback like:

public delegate void fn(ref IntPtr pData);

If correct, how do I manipulate the data passed to the callback? I've
tried accessing it like:

pData[0] = 230;

but this gives errors. I'd like to be able to modify the data directly if
needed. In some cases setting the pointer to a text message is preferred.

Second question: How are enumerated types stored memory-wise? I've got an
enumerated type that I pass to some of my callbacks. It's a fairly
straight-forward enum (i.e. starts with 0 and increments as would be
expected.) I can always use UInt32 if I need to.

Thanks,
Brian

Nov 16 '05 #7
Hi,

<Brian> wrote in message news:10*************@corp.supernews.com...
I've got two more questions, and *hopefully* that'll be all for a while.

First, I've got a character array (either signed or unsigned depending
on the usage) that will be passed to a callback. To complicate matters,
this character array is modifiable. I'm assuming that I should use define
my callback like:

public delegate void fn(ref IntPtr pData);
If the parameter was char** then this is fine.

If correct, how do I manipulate the data passed to the callback? I've
tried accessing it like:

pData[0] = 230;
You can use
- Marshal.WriteByte(pData, idx, 230);
- Marshal.ReadByte(pData, idx);

but this gives errors. I'd like to be able to modify the data directly if
needed. In some cases setting the pointer to a text message is preferred.
If you set a pointer to text or to anything you should allocate memory and
if you allocate memory you should free it not the dll, since this is a
callback, it might be difficult.

The problems start when eg. c# allocates memory and the dll must free it or
vice versa.

If you think it's possible to de-allocate it then you can use it:

pData = Marshal.StringToHGlobalAnsi ("kkmkml");

You should call Marshal.FreeHGlobal(pData) at some time...

Second question: How are enumerated types stored memory-wise? I've got an
enumerated type that I pass to some of my callbacks. It's a fairly
straight-forward enum (i.e. starts with 0 and increments as would be
expected.) I can always use UInt32 if I need to.
I don't know. I would start with uint and if everything is fine you can try
using an enum.
HTH
greetings

Thanks,
Brian

Nov 16 '05 #8
Hi,
Hi, <Brian> wrote in message news:10*************@corp.supernews.com...
I've got two more questions, and *hopefully* that'll be all for a while.

First, I've got a character array (either signed or unsigned depending
on the usage) that will be passed to a callback. To complicate matters,
this character array is modifiable. I'm assuming that I should use define
my callback like:

public delegate void fn(ref IntPtr pData);
If the parameter was char** then this is fine.
No, it's just a char *. How will that affect the prototype?

If correct, how do I manipulate the data passed to the callback? I've
tried accessing it like:

pData[0] = 230;

You can use
- Marshal.WriteByte(pData, idx, 230);
- Marshal.ReadByte(pData, idx);
Hmmm. Seems a bit cumbersome. It's doubtful that individual bytes would
*need* to be changed, so I'll leave that up to my end user.

What about writing the whole thing out to file? How would I accomplish
that now that you know it's a char * instead of a char **?
If you set a pointer to text or to anything you should allocate memory and
if you allocate memory you should free it not the dll, since this is a
callback, it might be difficult. The problems start when eg. c# allocates memory and the dll must free it or
vice versa.
I was afraid you might say that. The pointer passed to the callback is
also used internally by the DLL for performance reasons. Of course, this
data cannot be modified by the DLL before the callback returns.
pData = Marshal.StringToHGlobalAnsi ("kkmkml");
kkmkml? :)

I don't know. I would start with uint and if everything is fine you can try
using an enum.
Guess I'll find out the old-fashioned way. Gotta get my hammer ready.
HTH


Thanks. I appreciate your help. I'm understanding things much better
now.

Brian
Nov 16 '05 #9
Hi,

<Brian> wrote in message news:10*************@corp.supernews.com...
Hi,
Hi,
<Brian> wrote in message news:10*************@corp.supernews.com...
I've got two more questions, and *hopefully* that'll be all for a while.
First, I've got a character array (either signed or unsigned depending
on the usage) that will be passed to a callback. To complicate matters, this character array is modifiable. I'm assuming that I should use define my callback like:

public delegate void fn(ref IntPtr pData);
If the parameter was char** then this is fine.


No, it's just a char *. How will that affect the prototype?


Wel if it's just char* , then the callback function can't change the
pointer. You *can* change or read the data. The prototype should be just
IntPtr not ref IntPtr.

If correct, how do I manipulate the data passed to the callback? I've
tried accessing it like:

pData[0] = 230;
You can use
- Marshal.WriteByte(pData, idx, 230);
- Marshal.ReadByte(pData, idx);

This is still valid in case you need it.

Hmmm. Seems a bit cumbersome. It's doubtful that individual bytes would
*need* to be changed, so I'll leave that up to my end user.

What about writing the whole thing out to file? How would I accomplish
that now that you know it's a char * instead of a char **?
You can copy it into a string and write the string to a file :

string data = Marshal.PtrToStringAnsi( pData );
Stream s = File.OpenWrite( "test.txt" );
StreamWriter sw = new StreamWriter( s );
sw.Write ( data );
s.Close();

If you would know the size of the data then you could also put it inside a
byte[] and use a BinaryWriter :

byte [] data = new byte[size];
Marshal.Copy ( pData, data, 0, size );
Stream s = File.OpenWrite( "test.dat" );
BinaryWriter bw = new BinaryWriter( s );
bw.Write ( data );
s.Close();

If this isn't sufficient please ask again.
If you set a pointer to text or to anything you should allocate memory
and if you allocate memory you should free it not the dll, since this is a
callback, it might be difficult.

The problems start when eg. c# allocates memory and the dll must free it or vice versa.


I was afraid you might say that. The pointer passed to the callback is
also used internally by the DLL for performance reasons.
Of course, this
data cannot be modified by the DLL before the callback returns.
pData = Marshal.StringToHGlobalAnsi ("kkmkml");
This is *no longer valid*, to be able to change a pointer you need a pointer
to pointer.

If you want to change the data you should use lstrcpyA, WriteByte or
Marshal.Copy.
I've already demonstrated lstrcpyA and WriteByte in earlier reply.
Here is a example where you use Marshal.Copy to change the data.

byte [] ToWriteData = new byte[size];
// fill ToWriteData
Marshal.Copy ( ToWriteData, 0, pData, size );

kkmkml? :)
I was in a hurry :)

I don't know. I would start with uint and if everything is fine you can
try using an enum.


Guess I'll find out the old-fashioned way. Gotta get my hammer ready.


Don't worry. Remember that you can always cast an enum to an int if you
want to play safe.

HTH,
greetings

HTH


Thanks. I appreciate your help. I'm understanding things much better
now.

Brian

Nov 16 '05 #10
Hi,

I failed to mention that I'll be using an exported function within a
custom DLL that I'll need to pass the following structure to. Will there
be any problems with the garbage collection freeing up the pointers to my
callbacks? I need the callbacks to be available throughout the life-cycle
of the program. The structure itself can be discarded as my DLL simply
copies the contents into its own format.

basically...

// dll function prototype

void __stdcall AssignCallbacks(DemoStruct *pDemoStruct);

I'm guessing the C# code would be...

[DllImport("mydll.dll",CharSet=CharSet.Ansi)]
public static extern void AssignCallbacks(ref DemoStruct pDemoStruct);

.... and I would call it like so ...

void SimpleFunction()
{
// DemoStruct as is defined below...

ds = new DemoStruct();
ds.function1 = new DelFn1( MyFunction1 );
ds.function2 = new DelFn2( MyFunction2 );

AssignCallbacks(ds);
}

Thanks,
Brian

BMermuys <so*****@someone.com> wrote:
[StructLayout(StructLayout.Sequential)]
public struct DemoStruct
{
public DelFn1 function1;
public DelFn2 function2;
} [DllImport("kernel32.dll",CharSet=CharSet.Ansi)]
public static extern long lstrcpyA (IntPtr pDst, string sSrc); private bool MyFunction1(uint i, IntPtr pPath, uint pathLength)
{
// TODO check pathLength before writing
lstrcpyA (pPath, "unknown");
return true;
}

private bool MyFunction2(ref ToPassAsArgStruct s)
{
s.a = 5;
return true;
}

[DllImport("kernel32.dll",CharSet=CharSet.Auto)]
public static extern bool SetEnvironmentVariable (string sName, string
sValue);

private DemoStruct ds;
private IntPtr pGlobalText = IntPtr.Zero;

void SimpleFunction()
{
ds = new DemoStruct();
ds.function1 = new DelFn1( MyFunction1 );
ds.function2 = new DelFn2( MyFunction2 );

// Allocate unmanaged memory the dll can read/write
pGlobalText = Marshal.AllocHGlobal (5);

lstrcpyA ( pGlobalText, "test" ); // write

// pass pointer as hex string value
SetEnvironmentVariable("test", pGlobalText.ToInt32().ToString("X") );
}

* After this function you can :
- read from globaltext
Console.WriteLine ( Marshal.PtrToStringAnsi( pGlobalText ) ); /// READ - write to globaltext
lstrcpyA ( pGlobalText, text ); - cleanup globaltext
(only if dll doesn't need globaltext anymore)
Marshal.FreeHGlobal( pGlobalText );
HTH,


Believe me, it did. Thanks!
Brian
Nov 16 '05 #11
Hi,

<snip>
Wel if it's just char* , then the callback function can't change the
pointer. You *can* change or read the data. The prototype should be just
IntPtr not ref IntPtr.
>> If correct, how do I manipulate the data passed to the callback? I've
>> tried accessing it like:
>>
>> pData[0] = 230;

> You can use
> - Marshal.WriteByte(pData, idx, 230);
> - Marshal.ReadByte(pData, idx);
This is still valid in case you need it.
That's good. While *I* won't need it, there is a chance an end-user
might.

You can copy it into a string and write the string to a file :

string data = Marshal.PtrToStringAnsi( pData );
Stream s = File.OpenWrite( "test.txt" );
StreamWriter sw = new StreamWriter( s );
sw.Write ( data );
s.Close();

I suppose that I could also use import sprintf or some other function and
use the pointer with that. Correct? If so, based on your other code that
you've provided, I think I've got that one covered.

Out of morbid curiosity, any particular reason why C# is so 'protective'
of its data? In the land of C (sort of a contradiction there,
phonetically speaking), it's possible to do just about anything to
anybody. C# seems to want keep a tight reign of its data. I'm assuming
its for security reasons, but it sure makes it rough on the 'die-hard' C
programmers.

If you would know the size of the data then you could also put it inside a
byte[] and use a BinaryWriter :
I do know the size, so this is very helpful to know.
byte [] data = new byte[size];
Marshal.Copy ( pData, data, 0, size );
Stream s = File.OpenWrite( "test.dat" );
BinaryWriter bw = new BinaryWriter( s );
bw.Write ( data );
s.Close();

If this isn't sufficient please ask again.
I think that about covers it. Thanks.
I was afraid you might say that. The pointer passed to the callback is
also used internally by the DLL for performance reasons.
Of course, this data cannot be modified by the DLL before the callback
returns.


This is *no longer valid*, to be able to change a pointer you need a pointer
to pointer.

If you want to change the data you should use lstrcpyA, WriteByte or
Marshal.Copy.
I've already demonstrated lstrcpyA and WriteByte in earlier reply.
Here is a example where you use Marshal.Copy to change the data.


Perhaps I should read the entire reply before I respond. Thanks so much
for all the examples.
byte [] ToWriteData = new byte[size];
// fill ToWriteData
Marshal.Copy ( ToWriteData, 0, pData, size );

kkmkml? :)

I was in a hurry :)
Been there, done that.
Don't worry. Remember that you can always cast an enum to an int if you
want to play safe.
Yeah, but I'll at least see if it works. Never know til I try. :)
HTH,


It has. Immensely.

Thanks,
Brian
Nov 16 '05 #12
Hi,

<Brian> wrote in message news:10*************@corp.supernews.com...
Hi,

I failed to mention that I'll be using an exported function within a
custom DLL that I'll need to pass the following structure to. Will there
be any problems with the garbage collection freeing up the pointers to my
callbacks?
Yes if you don't keep a reference to the struct.

Make sure you can access the DemoStruct variable during the entire program
(this is the same as saying you should keep a reference somewhere). Then
the GC won't cleanup the delegates. As long as the delegates are alive and
not changed the function pointers remain valid.

Eg. declare demostruct as a instance variable or static variable in a class
(*not* as a local variable inside a function ).
I need the callbacks to be available throughout the life-cycle
of the program. The structure itself can be discarded as my DLL simply
Yes, your dll may copy the struct. But you must keep the managed struct
after 'assigncallbacks'.
copies the contents into its own format.

basically...

// dll function prototype

void __stdcall AssignCallbacks(DemoStruct *pDemoStruct);

I'm guessing the C# code would be...

[DllImport("mydll.dll",CharSet=CharSet.Ansi)]
public static extern void AssignCallbacks(ref DemoStruct pDemoStruct);
Yes that's it.

Don't hesitate to ask more if required.

hth,
greetings


... and I would call it like so ...

void SimpleFunction()
{
// DemoStruct as is defined below...

ds = new DemoStruct();
ds.function1 = new DelFn1( MyFunction1 );
ds.function2 = new DelFn2( MyFunction2 );

AssignCallbacks(ds);
}

Thanks,
Brian

BMermuys <so*****@someone.com> wrote:
[StructLayout(StructLayout.Sequential)]
public struct DemoStruct
{
public DelFn1 function1;
public DelFn2 function2;
}

[DllImport("kernel32.dll",CharSet=CharSet.Ansi)]
public static extern long lstrcpyA (IntPtr pDst, string sSrc);

private bool MyFunction1(uint i, IntPtr pPath, uint pathLength)
{
// TODO check pathLength before writing
lstrcpyA (pPath, "unknown");
return true;
}

private bool MyFunction2(ref ToPassAsArgStruct s)
{
s.a = 5;
return true;
}

[DllImport("kernel32.dll",CharSet=CharSet.Auto)]
public static extern bool SetEnvironmentVariable (string sName, string
sValue);

private DemoStruct ds;
private IntPtr pGlobalText = IntPtr.Zero;

void SimpleFunction()
{
ds = new DemoStruct();
ds.function1 = new DelFn1( MyFunction1 );
ds.function2 = new DelFn2( MyFunction2 );

// Allocate unmanaged memory the dll can read/write
pGlobalText = Marshal.AllocHGlobal (5);

lstrcpyA ( pGlobalText, "test" ); // write

// pass pointer as hex string value
SetEnvironmentVariable("test", pGlobalText.ToInt32().ToString("X") ); }

* After this function you can :
- read from globaltext
Console.WriteLine ( Marshal.PtrToStringAnsi( pGlobalText ) ); /// READ

- write to globaltext
lstrcpyA ( pGlobalText, text );

- cleanup globaltext
(only if dll doesn't need globaltext anymore)
Marshal.FreeHGlobal( pGlobalText );


HTH,


Believe me, it did. Thanks!
Brian

Nov 16 '05 #13
Thanks for the help. It is very much appreciated.

I figured that the GC might behave the way that you described, but keeping
an active reference shouldn't be too difficult.

Thanks,
Brian
Hi, <Brian> wrote in message news:10*************@corp.supernews.com...
Hi,

I failed to mention that I'll be using an exported function within a
custom DLL that I'll need to pass the following structure to. Will there
be any problems with the garbage collection freeing up the pointers to my
callbacks?
Yes if you don't keep a reference to the struct. Make sure you can access the DemoStruct variable during the entire program
(this is the same as saying you should keep a reference somewhere). Then
the GC won't cleanup the delegates. As long as the delegates are alive and
not changed the function pointers remain valid. Eg. declare demostruct as a instance variable or static variable in a class
(*not* as a local variable inside a function ).
I need the callbacks to be available throughout the life-cycle
of the program. The structure itself can be discarded as my DLL simply Yes, your dll may copy the struct. But you must keep the managed struct
after 'assigncallbacks'. copies the contents into its own format.

basically...

// dll function prototype

void __stdcall AssignCallbacks(DemoStruct *pDemoStruct);

I'm guessing the C# code would be...

[DllImport("mydll.dll",CharSet=CharSet.Ansi)]
public static extern void AssignCallbacks(ref DemoStruct pDemoStruct); Yes that's it. Don't hesitate to ask more if required. hth,
greetings



... and I would call it like so ...

void SimpleFunction()
{
// DemoStruct as is defined below...

ds = new DemoStruct();
ds.function1 = new DelFn1( MyFunction1 );
ds.function2 = new DelFn2( MyFunction2 );

AssignCallbacks(ds);
}

Thanks,
Brian

BMermuys <so*****@someone.com> wrote:
> [StructLayout(StructLayout.Sequential)]
> public struct DemoStruct
> {
> public DelFn1 function1;
> public DelFn2 function2;
> }

> [DllImport("kernel32.dll",CharSet=CharSet.Ansi)]
> public static extern long lstrcpyA (IntPtr pDst, string sSrc);

> private bool MyFunction1(uint i, IntPtr pPath, uint pathLength)
> {
> // TODO check pathLength before writing
> lstrcpyA (pPath, "unknown");
> return true;
> }
>
> private bool MyFunction2(ref ToPassAsArgStruct s)
> {
> s.a = 5;
> return true;
> }
>
> [DllImport("kernel32.dll",CharSet=CharSet.Auto)]
> public static extern bool SetEnvironmentVariable (string sName, string
> sValue);
>
> private DemoStruct ds;
> private IntPtr pGlobalText = IntPtr.Zero;
>
> void SimpleFunction()
> {
> ds = new DemoStruct();
> ds.function1 = new DelFn1( MyFunction1 );
> ds.function2 = new DelFn2( MyFunction2 );
>
> // Allocate unmanaged memory the dll can read/write
> pGlobalText = Marshal.AllocHGlobal (5);
>
> lstrcpyA ( pGlobalText, "test" ); // write
>
> // pass pointer as hex string value
> SetEnvironmentVariable("test",

pGlobalText.ToInt32().ToString("X") ); > }
>
> * After this function you can :
> - read from globaltext
> Console.WriteLine ( Marshal.PtrToStringAnsi( pGlobalText ) ); /// READ

> - write to globaltext
> lstrcpyA ( pGlobalText, text );

> - cleanup globaltext
> (only if dll doesn't need globaltext anymore)
> Marshal.FreeHGlobal( pGlobalText );


> HTH,


Believe me, it did. Thanks!
Brian


--
Brian
Nov 16 '05 #14
I've got two more questions, and *hopefully* that'll be all for a while.

First, I've got a character array (either signed or unsigned depending
on the usage) that will be passed to a callback. To complicate matters,
this character array is modifiable. I'm assuming that I should use define
my callback like:

public delegate void fn(ref IntPtr pData);

If correct, how do I manipulate the data passed to the callback? I've
tried accessing it like:

pData[0] = 230;

but this gives errors. I'd like to be able to modify the data directly if
needed. In some cases setting the pointer to a text message is preferred.

Second question: How are enumerated types stored memory-wise? I've got an
enumerated type that I pass to some of my callbacks. It's a fairly
straight-forward enum (i.e. starts with 0 and increments as would be
expected.) I can always use UInt32 if I need to.

Thanks,
Brian

Nov 16 '05 #15
Hi,

<Brian> wrote in message news:10*************@corp.supernews.com...
I've got two more questions, and *hopefully* that'll be all for a while.

First, I've got a character array (either signed or unsigned depending
on the usage) that will be passed to a callback. To complicate matters,
this character array is modifiable. I'm assuming that I should use define
my callback like:

public delegate void fn(ref IntPtr pData);
If the parameter was char** then this is fine.

If correct, how do I manipulate the data passed to the callback? I've
tried accessing it like:

pData[0] = 230;
You can use
- Marshal.WriteByte(pData, idx, 230);
- Marshal.ReadByte(pData, idx);

but this gives errors. I'd like to be able to modify the data directly if
needed. In some cases setting the pointer to a text message is preferred.
If you set a pointer to text or to anything you should allocate memory and
if you allocate memory you should free it not the dll, since this is a
callback, it might be difficult.

The problems start when eg. c# allocates memory and the dll must free it or
vice versa.

If you think it's possible to de-allocate it then you can use it:

pData = Marshal.StringToHGlobalAnsi ("kkmkml");

You should call Marshal.FreeHGlobal(pData) at some time...

Second question: How are enumerated types stored memory-wise? I've got an
enumerated type that I pass to some of my callbacks. It's a fairly
straight-forward enum (i.e. starts with 0 and increments as would be
expected.) I can always use UInt32 if I need to.
I don't know. I would start with uint and if everything is fine you can try
using an enum.
HTH
greetings

Thanks,
Brian

Nov 16 '05 #16
Hi,
Hi, <Brian> wrote in message news:10*************@corp.supernews.com...
I've got two more questions, and *hopefully* that'll be all for a while.

First, I've got a character array (either signed or unsigned depending
on the usage) that will be passed to a callback. To complicate matters,
this character array is modifiable. I'm assuming that I should use define
my callback like:

public delegate void fn(ref IntPtr pData);
If the parameter was char** then this is fine.
No, it's just a char *. How will that affect the prototype?

If correct, how do I manipulate the data passed to the callback? I've
tried accessing it like:

pData[0] = 230;

You can use
- Marshal.WriteByte(pData, idx, 230);
- Marshal.ReadByte(pData, idx);
Hmmm. Seems a bit cumbersome. It's doubtful that individual bytes would
*need* to be changed, so I'll leave that up to my end user.

What about writing the whole thing out to file? How would I accomplish
that now that you know it's a char * instead of a char **?
If you set a pointer to text or to anything you should allocate memory and
if you allocate memory you should free it not the dll, since this is a
callback, it might be difficult. The problems start when eg. c# allocates memory and the dll must free it or
vice versa.
I was afraid you might say that. The pointer passed to the callback is
also used internally by the DLL for performance reasons. Of course, this
data cannot be modified by the DLL before the callback returns.
pData = Marshal.StringToHGlobalAnsi ("kkmkml");
kkmkml? :)

I don't know. I would start with uint and if everything is fine you can try
using an enum.
Guess I'll find out the old-fashioned way. Gotta get my hammer ready.
HTH


Thanks. I appreciate your help. I'm understanding things much better
now.

Brian
Nov 16 '05 #17
Hi,

<Brian> wrote in message news:10*************@corp.supernews.com...
Hi,
Hi,
<Brian> wrote in message news:10*************@corp.supernews.com...
I've got two more questions, and *hopefully* that'll be all for a while.
First, I've got a character array (either signed or unsigned depending
on the usage) that will be passed to a callback. To complicate matters, this character array is modifiable. I'm assuming that I should use define my callback like:

public delegate void fn(ref IntPtr pData);
If the parameter was char** then this is fine.


No, it's just a char *. How will that affect the prototype?


Wel if it's just char* , then the callback function can't change the
pointer. You *can* change or read the data. The prototype should be just
IntPtr not ref IntPtr.

If correct, how do I manipulate the data passed to the callback? I've
tried accessing it like:

pData[0] = 230;
You can use
- Marshal.WriteByte(pData, idx, 230);
- Marshal.ReadByte(pData, idx);

This is still valid in case you need it.

Hmmm. Seems a bit cumbersome. It's doubtful that individual bytes would
*need* to be changed, so I'll leave that up to my end user.

What about writing the whole thing out to file? How would I accomplish
that now that you know it's a char * instead of a char **?
You can copy it into a string and write the string to a file :

string data = Marshal.PtrToStringAnsi( pData );
Stream s = File.OpenWrite( "test.txt" );
StreamWriter sw = new StreamWriter( s );
sw.Write ( data );
s.Close();

If you would know the size of the data then you could also put it inside a
byte[] and use a BinaryWriter :

byte [] data = new byte[size];
Marshal.Copy ( pData, data, 0, size );
Stream s = File.OpenWrite( "test.dat" );
BinaryWriter bw = new BinaryWriter( s );
bw.Write ( data );
s.Close();

If this isn't sufficient please ask again.
If you set a pointer to text or to anything you should allocate memory
and if you allocate memory you should free it not the dll, since this is a
callback, it might be difficult.

The problems start when eg. c# allocates memory and the dll must free it or vice versa.


I was afraid you might say that. The pointer passed to the callback is
also used internally by the DLL for performance reasons.
Of course, this
data cannot be modified by the DLL before the callback returns.
pData = Marshal.StringToHGlobalAnsi ("kkmkml");
This is *no longer valid*, to be able to change a pointer you need a pointer
to pointer.

If you want to change the data you should use lstrcpyA, WriteByte or
Marshal.Copy.
I've already demonstrated lstrcpyA and WriteByte in earlier reply.
Here is a example where you use Marshal.Copy to change the data.

byte [] ToWriteData = new byte[size];
// fill ToWriteData
Marshal.Copy ( ToWriteData, 0, pData, size );

kkmkml? :)
I was in a hurry :)

I don't know. I would start with uint and if everything is fine you can
try using an enum.


Guess I'll find out the old-fashioned way. Gotta get my hammer ready.


Don't worry. Remember that you can always cast an enum to an int if you
want to play safe.

HTH,
greetings

HTH


Thanks. I appreciate your help. I'm understanding things much better
now.

Brian

Nov 16 '05 #18
Hi,

<snip>
Wel if it's just char* , then the callback function can't change the
pointer. You *can* change or read the data. The prototype should be just
IntPtr not ref IntPtr.
>> If correct, how do I manipulate the data passed to the callback? I've
>> tried accessing it like:
>>
>> pData[0] = 230;

> You can use
> - Marshal.WriteByte(pData, idx, 230);
> - Marshal.ReadByte(pData, idx);
This is still valid in case you need it.
That's good. While *I* won't need it, there is a chance an end-user
might.

You can copy it into a string and write the string to a file :

string data = Marshal.PtrToStringAnsi( pData );
Stream s = File.OpenWrite( "test.txt" );
StreamWriter sw = new StreamWriter( s );
sw.Write ( data );
s.Close();

I suppose that I could also use import sprintf or some other function and
use the pointer with that. Correct? If so, based on your other code that
you've provided, I think I've got that one covered.

Out of morbid curiosity, any particular reason why C# is so 'protective'
of its data? In the land of C (sort of a contradiction there,
phonetically speaking), it's possible to do just about anything to
anybody. C# seems to want keep a tight reign of its data. I'm assuming
its for security reasons, but it sure makes it rough on the 'die-hard' C
programmers.

If you would know the size of the data then you could also put it inside a
byte[] and use a BinaryWriter :
I do know the size, so this is very helpful to know.
byte [] data = new byte[size];
Marshal.Copy ( pData, data, 0, size );
Stream s = File.OpenWrite( "test.dat" );
BinaryWriter bw = new BinaryWriter( s );
bw.Write ( data );
s.Close();

If this isn't sufficient please ask again.
I think that about covers it. Thanks.
I was afraid you might say that. The pointer passed to the callback is
also used internally by the DLL for performance reasons.
Of course, this data cannot be modified by the DLL before the callback
returns.


This is *no longer valid*, to be able to change a pointer you need a pointer
to pointer.

If you want to change the data you should use lstrcpyA, WriteByte or
Marshal.Copy.
I've already demonstrated lstrcpyA and WriteByte in earlier reply.
Here is a example where you use Marshal.Copy to change the data.


Perhaps I should read the entire reply before I respond. Thanks so much
for all the examples.
byte [] ToWriteData = new byte[size];
// fill ToWriteData
Marshal.Copy ( ToWriteData, 0, pData, size );

kkmkml? :)

I was in a hurry :)
Been there, done that.
Don't worry. Remember that you can always cast an enum to an int if you
want to play safe.
Yeah, but I'll at least see if it works. Never know til I try. :)
HTH,


It has. Immensely.

Thanks,
Brian
Nov 16 '05 #19
Hi,

<Brian> wrote in message news:10*************@corp.supernews.com...
Hi,

<snip>
I suppose that I could also use import sprintf or some other function and
use the pointer with that. Correct? If so, based on your other code that
you've provided, I think I've got that one covered.
You could use sprintf (c/c++ runtime) but there is a difference in calling
convention, sprintf uses __cdecl. You can call it from c# but you can't
have variable arguments therefore all arguments must be declared eg :

[DllImport("msvcrt.dll", CharSet=CharSet.Ansi,
CallingConvention=CallingConvention.Cdecl)]
public static extern int sprintf(IntPtr pDest, String format, int i, String
s);

And I don't think you need it, you can use .Net formatting capabilities :
string s = Format( "{0} {1}", 10, "test" ); // eg
lstrcpyA( pData, s );

Out of morbid curiosity, any particular reason why C# is so 'protective'
It's not just c#, all net languages compile to ilasm where most of the
restrictions come from.
The idea is to produce more secure and stable applications.
of its data? In the land of C (sort of a contradiction there,
phonetically speaking), it's possible to do just about anything to
anybody.
Altough I like c it leads to hard to find bugs.
C# seems to want keep a tight reign of its data. I'm assuming
its for security reasons, but it sure makes it rough on the 'die-hard' C
programmers.


You should just forget about pointers when you use c# and learn the class
library which has a lot of functionality.

hth,
Greetings
Nov 16 '05 #20
> You could use sprintf (c/c++ runtime) but there is a difference in calling
convention, sprintf uses __cdecl. You can call it from c# but you can't
have variable arguments therefore all arguments must be declared eg :
Oh, yeah.... I forgot about that little gotcha.
[DllImport("msvcrt.dll", CharSet=CharSet.Ansi,
CallingConvention=CallingConvention.Cdecl)]
public static extern int sprintf(IntPtr pDest, String format, int i, String
s); And I don't think you need it, you can use .Net formatting capabilities :
string s = Format( "{0} {1}", 10, "test" ); // eg
lstrcpyA( pData, s );
That's what I'll recommend should anyone ask about it. Obviously, sprintf
would be a pain to use.


Out of morbid curiosity, any particular reason why C# is so 'protective' It's not just c#, all net languages compile to ilasm where most of the
restrictions come from.
The idea is to produce more secure and stable applications. of its data? In the land of C (sort of a contradiction there,
phonetically speaking), it's possible to do just about anything to
anybody. Altough I like c it leads to hard to find bugs.
But entomology is fun! :)

C# seems to want keep a tight reign of its data. I'm assuming
its for security reasons, but it sure makes it rough on the 'die-hard' C
programmers.

You should just forget about pointers when you use c# and learn the class
library which has a lot of functionality.


I shall forget. Now what were we talking about??? :) Seriously though,
I've got a couple of books on C#, so hopefully those plus the very helpful
suggestions I've gotten from you will be enough to see me through.

Thanks again,
Brian
Nov 16 '05 #21
I had a problem with the lstrcpyA function. I watched the memory on the
destination string, and only the first character of the src string was
copied due to its unicode nature. I wrote the following function to take
care of the issue. Is there a better way. The destination string is a
non-unicode string.

public int CopyString(string src, IntPtr dest, UInt32 len)
{
byte [] copiedSrc;
if (len < src.Length)
{
return 0;
}

copiedSrc = new byte [src.Length];

for (int i=0; i<src.Length; i++)
{
byte b = (byte)src[i];
copiedSrc[i] = b;
}

lstrcpyA(dest, copiedSrc);

return src.Length;
}
<big snip>

Thanks,
Brian
Nov 16 '05 #22
Hi,

<Brian> wrote in message news:10*************@corp.supernews.com...
I had a problem with the lstrcpyA function. I watched the memory on the
destination string, and only the first character of the src string was
copied due to its unicode nature.
[DllImport(@"kernel32.dll", CharSet=CharSet.Ansi)]
public extern static void lstrcpyA(IntPtr p, string s);

Because of the CharSet.Ansi parameter the framework should first re-code the
unicode string (s) into an ansi one before copy. It works for me, don't
know why you see this behaviour.
I wrote the following function to take
care of the issue. Is there a better way. The destination string is a
non-unicode string.
There's a better way (managed) to convert an unicode string into ansi bytes
and then you don't need lstrcpyA anymore but you can use Marshal.Copy :

public int CopyString( string src, IntPtr dest, UInt32 len )
{
// truncate if src too long
if ( src.Length > (len-1) ) src = src.Substring(0, len-1);

// Get ansi bytes including null terminator
byte [] temp = System.Text.Encoding.Default.GetBytes( src + '\0' );

Marshal.Copy( temp, 0, dest, temp.Length );
return src.Length;
}

hth,
Greetings

public int CopyString(string src, IntPtr dest, UInt32 len)
{
byte [] copiedSrc;
if (len < src.Length)
{
return 0;
}

copiedSrc = new byte [src.Length];

for (int i=0; i<src.Length; i++)
{
byte b = (byte)src[i];
copiedSrc[i] = b;
}

lstrcpyA(dest, copiedSrc);

return src.Length;
}
<big snip>

Thanks,
Brian

Nov 16 '05 #23
BMermuys <so*****@someone.com> wrote:
[DllImport(@"kernel32.dll", CharSet=CharSet.Ansi)]
public extern static void lstrcpyA(IntPtr p, string s);


Oh, yeah. Perhaps if I used 'Ansi' instead of 'Auto' things might work
out a bit better. Must've been late that night. Sorry for the bother.

Appreciate the improved CopyString code, too. Looks like I probably won't
need it now. Maybe I'll feel smarter tomorrow. :(

Hey, out of curiosity, what's the @ in the @"kernel32.dll" for? The code
compiles fine without it. Is it necessary? My books don't mention it in
this context.

Thanks,
Brian
Nov 16 '05 #24

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

Similar topics

22
by: Brian | last post by:
I am very new to C# programming and have run into a problem. I apologize for the repost of this article. For some reason, my news reader attached it to an existing thread. First off, I have...
25
by: Brian | last post by:
I am very new to C# programming and have run into a problem. I apologize for the repost of this article. For some reason, my news reader attached it to an existing thread. First off, I have...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.