Click here to Skip to main content
15,889,211 members
Home / Discussions / C#
   

C#

 
GeneralRe: Error: Calling C++ dll function in C# Pin
Richard MacCutchan22-Oct-12 1:05
mveRichard MacCutchan22-Oct-12 1:05 
GeneralRe: Error: Calling C++ dll function in C# Pin
taibc22-Oct-12 0:37
taibc22-Oct-12 0:37 
GeneralRe: Error: Calling C++ dll function in C# Pin
Richard MacCutchan22-Oct-12 1:03
mveRichard MacCutchan22-Oct-12 1:03 
AnswerRe: Error: Calling C++ dll function in C# Pin
BobJanova22-Oct-12 4:20
BobJanova22-Oct-12 4:20 
GeneralRe: Error: Calling C++ dll function in C# Pin
taibc22-Oct-12 15:51
taibc22-Oct-12 15:51 
GeneralRe: Error: Calling C++ dll function in C# Pin
taibc22-Oct-12 16:43
taibc22-Oct-12 16:43 
GeneralRe: Error: Calling C++ dll function in C# Pin
taibc23-Oct-12 17:44
taibc23-Oct-12 17:44 
AnswerRe: Error: Calling C++ dll function in C# Pin
taibc22-Oct-12 23:04
taibc22-Oct-12 23:04 
I am getting another error when casting between two structs in C#:

I have the function in C:
C++
__declspec(dllexport) DGNElemCore CPL_DLL *DGNReadElement( DGNHandle );


This function will read a file and return a pointer to either DGNElemCore or DGNElemText, and I can use casting between them.

But, when I convert codes into C# as below, I can't cast types of these structs:

C#
[DllImport("DgnLib.dll", EntryPoint = "DGNReadElement")]              
        public static extern IntPtr DGNReadElement(IntPtr DGNHandle);    
 public static DGNElemCore ReadElement(IntPtr DGNHandle)
        {
            DGNElemCore element = new DGNElemCore ();
            IntPtr itr = DGNReadElement(DGNHandle);
            MessageBox.Show(itr.GetType ().ToString ());

            element = (DGNElemCore)Marshal.PtrToStructure(itr, typeof(DGNElemCore));                          
            return element;            
        }


And the codes for testing:

C#
DGNElemCore element = new DGNElemCore();
element = DgnFile.ReadElement(DGNHandle);

while (element != null) 
{
    if (element.type == 17 ) // Element is a DGNElemText 
    {
         DGNElemText txtElement = new DGNElemText();
         txtElement = (DGNElemText)element; **// throw error: Unable to cast object of type DgnLib.DGNElemCore to type DgnLib.DGNElemText**

         ...... // More codes

    }
}


Structs in C#:

C#
[StructLayout(LayoutKind.Sequential )]
    public class DGNElemCore
    {
        public int offset;
        public int size;
        public int element_id;
        public int stype;
        public int level;
        public int type;
        public int complex;
        public int deleted;
        public int graphic_group;
        public int properties;
        public int color;
        public int weight;
        public int style;
        public int attr_bytes;
        public IntPtr attr_data;
        public int raw_bytes;
        public IntPtr raw_data;

    }

[StructLayout (LayoutKind.Sequential)]
    public class DGNElemText : DGNElemCore
    {
        // Fields
        DGNElemCore core;

        public int font_id;
        public int justification;
        public double length_mult;
        public double height_mult;
        public double rotation;
        public DGNPoint origin;
        public string text;
    }

GeneralRe: Error: Calling C++ dll function in C# Pin
mphill474423-Oct-12 7:10
mphill474423-Oct-12 7:10 
GeneralRe: Error: Calling C++ dll function in C# Pin
taibc23-Oct-12 17:07
taibc23-Oct-12 17:07 
Questionsmall IRC Based command shell Pin
seangroves21-Oct-12 13:42
seangroves21-Oct-12 13:42 
AnswerRe: small IRC Based command shell Pin
Richard MacCutchan21-Oct-12 21:54
mveRichard MacCutchan21-Oct-12 21:54 
Questionelse if statement vs switch statement Pin
DeAd_HeAd21-Oct-12 12:18
DeAd_HeAd21-Oct-12 12:18 
AnswerRe: else if statement vs switch statement Pin
Richard Andrew x6421-Oct-12 14:52
professionalRichard Andrew x6421-Oct-12 14:52 
GeneralRe: else if statement vs switch statement Pin
DeAd_HeAd21-Oct-12 16:07
DeAd_HeAd21-Oct-12 16:07 
GeneralRe: else if statement vs switch statement Pin
Richard Andrew x6421-Oct-12 16:17
professionalRichard Andrew x6421-Oct-12 16:17 
GeneralRe: else if statement vs switch statement Pin
Richard Deeming22-Oct-12 2:05
mveRichard Deeming22-Oct-12 2:05 
JokeRe: else if statement vs switch statement Pin
BobJanova22-Oct-12 4:24
BobJanova22-Oct-12 4:24 
AnswerRe: else if statement vs switch statement Pin
Abhinav S21-Oct-12 17:47
Abhinav S21-Oct-12 17:47 
AnswerRe: else if statement vs switch statement Pin
Kevin Bewley21-Oct-12 23:34
Kevin Bewley21-Oct-12 23:34 
GeneralRe: else if statement vs switch statement Pin
BobJanova22-Oct-12 4:24
BobJanova22-Oct-12 4:24 
Questionparameter comments for a method wont work fine Pin
mohammadkaab21-Oct-12 8:28
mohammadkaab21-Oct-12 8:28 
AnswerRe: parameter comments for a method wont work fine Pin
Eddy Vluggen21-Oct-12 22:17
professionalEddy Vluggen21-Oct-12 22:17 
GeneralRe: parameter comments for a method wont work fine Pin
mohammadkaab22-Oct-12 0:34
mohammadkaab22-Oct-12 0:34 
GeneralRe: parameter comments for a method wont work fine Pin
Eddy Vluggen22-Oct-12 0:52
professionalEddy Vluggen22-Oct-12 0:52 

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.