Click here to Skip to main content
15,887,474 members
Home / Discussions / C#
   

C#

 
AnswerRe: deleting a file from local disk after putting in DB Pin
leppie24-Jun-08 21:57
leppie24-Jun-08 21:57 
Questiondistributed computing?? Pin
Camilo Sanchez24-Jun-08 20:25
Camilo Sanchez24-Jun-08 20:25 
AnswerRe: distributed computing?? Pin
Marek Grzenkowicz24-Jun-08 22:49
Marek Grzenkowicz24-Jun-08 22:49 
GeneralRe: distributed computing?? Pin
Camilo Sanchez25-Jun-08 16:53
Camilo Sanchez25-Jun-08 16:53 
GeneralRe: distributed computing?? Pin
Marek Grzenkowicz26-Jun-08 1:07
Marek Grzenkowicz26-Jun-08 1:07 
QuestionUSP Implemenation Pin
sher_azam24-Jun-08 20:22
sher_azam24-Jun-08 20:22 
AnswerRe: USP Implemenation Pin
Ashfield24-Jun-08 21:14
Ashfield24-Jun-08 21:14 
QuestionCannot marshal 'return value': Pointers cannot reference marshaled structures? Pin
cristi_alonso24-Jun-08 20:18
cristi_alonso24-Jun-08 20:18 
<pre>--------------------------------------------------------------------------------

I wrote code to access structre from C# .net where structure is in C dll .here are the two files .when i debug the code i got runtime exception i.e MarshalDirectiveException saying that Cannot marshal 'return value': Pointers cannot reference marshaled structures. Use ByRef instead.


------------------------- StrcutreDisplay.dll---------------------------------

#include<iostream.h>

#include<conio.h>

#include<malloc.h>

#include<string.h>

extern "C"

{

typedef struct

{

char *fname;

char *lname;


char abc[5];

}Detail;




__declspec(dllexport) Detail * Disp()

{

Detail *ptr=(Detail *)malloc(sizeof(Detail));

ptr->fname = (char*)malloc(50);

memset(ptr->fname, 0x00, 50);

ptr->lname = (char*)malloc(50);

memset(ptr->lname, 0x00, 50);


memset(ptr->abc, 0x00, 5);

strcpy(ptr->fname, "larson");

strcpy(ptr->lname, "scolari");


strcpy(ptr->abc, "ABC");
return ptr;

}

}

----------------------------StructureExample---------------------------------





using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.InteropServices;

namespace StructureExample

{

class Program

{

[StructLayout(LayoutKind.Explicit)]

public struct Detail

{ // [MarshalAs(UnmanagedType.LPStr)]

public unsafe sbyte* fname;

public unsafe sbyte* lname;

[MarshalAs (UnmanagedType.ByValArray,SizeConst=5)]
public unsafe fixed char abc[5];

}





// strucure DLL

[DllImport("StructureDisplay.dll")]

public static unsafe extern Detail * Disp();

static void Main(string[] args)

{

unsafe

{

Detail * var = Disp();//Cannot marshal 'return value': Pointers cannot reference marshaled structures. Use ByRef instead.


string a = new string(var->fname);

string b = new string(var->lname);



string e = new string(var->abc);


}


Console.WriteLine("hello world");


Console.WriteLine(a);

Console.WriteLine(b);



Console.WriteLine(e);


Console.Read();

}

}

}


------------------------------------------------------------------------------------------------


Can anyone help me out to solve this problem?what i need to do to solve this problem?

Any help appreciated.
Thanks in advance.</pre>
AnswerRe: Cannot marshal 'return value': Pointers cannot reference marshaled structures? Pin
leppie24-Jun-08 22:42
leppie24-Jun-08 22:42 
Questionconnect network PC Pin
Rinki Mukheraji24-Jun-08 19:38
Rinki Mukheraji24-Jun-08 19:38 
AnswerRe: Duplicate Post : Cp Member Please Ignore Pin
Abhijit Jana24-Jun-08 21:43
professionalAbhijit Jana24-Jun-08 21:43 
Questionchecking an object is compatible with another object Pin
Mike Bentzen24-Jun-08 17:23
Mike Bentzen24-Jun-08 17:23 
AnswerRe: checking an object is compatible with another object Pin
Karthik Kalyanasundaram24-Jun-08 18:22
Karthik Kalyanasundaram24-Jun-08 18:22 
AnswerRe: checking an object is compatible with another object Pin
Christian Graus24-Jun-08 18:23
protectorChristian Graus24-Jun-08 18:23 
QuestionRe: checking an object is compatible with another object Pin
Mike Bentzen24-Jun-08 19:04
Mike Bentzen24-Jun-08 19:04 
AnswerRe: checking an object is compatible with another object Pin
Christian Graus24-Jun-08 19:07
protectorChristian Graus24-Jun-08 19:07 
GeneralRe: checking an object is compatible with another object Pin
Mike Bentzen24-Jun-08 19:25
Mike Bentzen24-Jun-08 19:25 
GeneralRe: checking an object is compatible with another object Pin
Christian Graus24-Jun-08 19:31
protectorChristian Graus24-Jun-08 19:31 
GeneralRe: checking an object is compatible with another object Pin
Mike Bentzen24-Jun-08 19:46
Mike Bentzen24-Jun-08 19:46 
GeneralRe: checking an object is compatible with another object Pin
Christian Graus24-Jun-08 19:57
protectorChristian Graus24-Jun-08 19:57 
GeneralRe: checking an object is compatible with another object Pin
Mike Bentzen24-Jun-08 20:05
Mike Bentzen24-Jun-08 20:05 
GeneralRe: checking an object is compatible with another object Pin
Christian Graus24-Jun-08 20:10
protectorChristian Graus24-Jun-08 20:10 
AnswerRe: checking an object is compatible with another object Pin
leppie24-Jun-08 22:30
leppie24-Jun-08 22:30 
AnswerRe: checking an object is compatible with another object Pin
PIEBALDconsult25-Jun-08 4:40
mvePIEBALDconsult25-Jun-08 4:40 
Questionforeach statement Pin
twistz24-Jun-08 16:12
twistz24-Jun-08 16:12 

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.