Click here to Skip to main content
16,006,707 members
Home / Discussions / C#
   

C#

 
GeneralRe: OleDbDataReader doubt Pin
Nish Nishant23-Dec-02 3:50
sitebuilderNish Nishant23-Dec-02 3:50 
GeneralRe: OleDbDataReader doubt Pin
Rocky Moore26-Dec-02 21:48
Rocky Moore26-Dec-02 21:48 
GeneralWindows Installer Pin
Jassim Rahma23-Dec-02 3:15
Jassim Rahma23-Dec-02 3:15 
GeneralRe: Windows Installer Pin
Nish Nishant23-Dec-02 3:42
sitebuilderNish Nishant23-Dec-02 3:42 
GeneralRe: Windows Installer Pin
James T. Johnson23-Dec-02 10:06
James T. Johnson23-Dec-02 10:06 
GeneralRe: Windows Installer Pin
Stephane Rodriguez.23-Dec-02 11:05
Stephane Rodriguez.23-Dec-02 11:05 
GeneralRe: Windows Installer Pin
James T. Johnson23-Dec-02 11:17
James T. Johnson23-Dec-02 11:17 
GeneralManaging the unmanageable Pin
Mauricio Ritter23-Dec-02 2:04
Mauricio Ritter23-Dec-02 2:04 
I have this function exported in a Windows API DLL written in C:

TESTECS_API int fnTeste(char* pOutput)
{
	char buffer[] = "0000MENSAGEM DE TESTE  ";
	memcpy(pOutput, buffer, sizeof(buffer));
	return 0;
}


I need to call this function from a managed C# code, passing a structure as the char* parameter. The parameter cosists of a set of strings, each one representing a piece of that BIG string that is being copied to the strucutre in the unmanaged DLL.
Here is how I defined the code to call the DLL in managed code:

[StructLayout(LayoutKind.Sequential , CharSet=CharSet.Ansi)]
public struct Resposta {
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst=2)] public String FIELD1;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst=2)] public String FIELD2;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst=20)] public String Mensagem;
}

public class Win32API {
[DllImport("testecs.Dll")]
public static extern void fnTeste( ref Resposta r);
}


public class App
{

	public static void Main()
	{
		Resposta 	r = new Resposta();

		Win32API.fnTeste(ref r);
		
		Console.WriteLine(r.FIELD1);
		Console.WriteLine(r.FIELD2);
		Console.WriteLine(r.Mensagem);	
	}	
}


The result that comes out of this program is:

0
0
MENSAGEM DE TESTE


The result I´m expeting is:

00
00
MENSAGEM DE TESTE


The problem is that the marshaller is putting an \0 in the end of each string of the strucutre, even if I state that is a ByValTStr and implicit tells its size. Does anyone know how to "automatically" remove te zero terminator during the marshalling process, so that I get the expected result ? Does anyone know any other method that I can use to solve this problem ?

thanks in advance

Mauricio Ritter - Brazil
Sonorking now: 100.13560 MRitter

"Th@ langwagje is screwed! It has if's but no end if's!! Stupid php cant even do butuns on forms! VISHAUL BASICS ARE THE FUTSHURE!" - Simon Walton
GeneralRe: Managing the unmanageable Pin
leppie23-Dec-02 12:07
leppie23-Dec-02 12:07 
GeneralGrid Control free sources Pin
Alex Gusev22-Dec-02 22:51
Alex Gusev22-Dec-02 22:51 
GeneralRe: Grid Control free sources Pin
James T. Johnson22-Dec-02 23:05
James T. Johnson22-Dec-02 23:05 
GeneralRe: Grid Control free sources Pin
Alex Gusev22-Dec-02 23:19
Alex Gusev22-Dec-02 23:19 
GeneralRe: Grid Control free sources Pin
James T. Johnson22-Dec-02 23:27
James T. Johnson22-Dec-02 23:27 
GeneralRe: Grid Control free sources Pin
Kannan Kalyanaraman23-Dec-02 18:16
Kannan Kalyanaraman23-Dec-02 18:16 
GeneralRe: Grid Control free sources Pin
James T. Johnson23-Dec-02 18:32
James T. Johnson23-Dec-02 18:32 
GeneralRe: Grid Control free sources Pin
Kannan Kalyanaraman23-Dec-02 22:23
Kannan Kalyanaraman23-Dec-02 22:23 
GeneralCrystal preview window tooltips Pin
vickycao22-Dec-02 21:52
vickycao22-Dec-02 21:52 
GeneralIBM Via Voice Pin
Jassim Rahma22-Dec-02 19:54
Jassim Rahma22-Dec-02 19:54 
GeneralRe: IBM Via Voice Pin
Kannan Kalyanaraman23-Dec-02 18:12
Kannan Kalyanaraman23-Dec-02 18:12 
GeneralCreate user as another user Pin
Mazdak22-Dec-02 19:24
Mazdak22-Dec-02 19:24 
QuestionBeginner Q: Custom Control, event forwarding? Pin
matthias s.22-Dec-02 12:17
matthias s.22-Dec-02 12:17 
Generalembedding mozilla in a C# app Pin
spacemonkey22-Dec-02 11:23
spacemonkey22-Dec-02 11:23 
GeneralRe: embedding mozilla in a C# app Pin
Stephane Rodriguez.23-Dec-02 1:42
Stephane Rodriguez.23-Dec-02 1:42 
GeneralRe: embedding mozilla in a C# app Pin
spacemonkey29-Dec-02 16:40
spacemonkey29-Dec-02 16:40 
GeneralAccess .mdb files Pin
Omega50122-Dec-02 9:50
Omega50122-Dec-02 9:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.