CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2007
    Posts
    28

    Help Porting C/C++ Function to C#

    Hi, this is the problem i am having:

    I have a PC104 with a ADO card. API's are provided to control ADO card, API is a C Compiled DLL. The example code that shows how to use this API is in C++ and as a initial test i want to port some basic fuctions to C#.

    This is whats happening:

    C++ Code:

    Code:
    void main(void)
    {
       char szBuf[512];
    
       if (!OpenBoard6430(BOARD_NO,0x6430,szBuf, &boardconfig))
          {
          cprintf("\n%s",szBuf);
          cprintf("\nIf an other DM6430 application is running,"
                        " please close it and try again.");
          getch();
          return;
          }
        DEVICE_NO = boardconfig.device_no;
    
    //This is the method signature in the H file:
    dllExport BOOL OpenBoard6430(int num, int device_id, LPSTR szBuf,
    		BoardConfig *boardconfig );
    //BoardConfig is a simple struct with 2 members.
    
    }
    This is the C# code i wrote:

    Code:
    public char[] szBuf = new char[512];
            public struct BoardConfig
            {
                int device_no;
                int it_no;
            }
            BoardConfig board;
            [DllImport("drvr6430.dll")]
            public static extern bool OpenBoard6430(int num, int id, char[] szBuf, BoardConfig board);
            
            private void button1_Click(object sender, EventArgs e)
            {
                if (!OpenBoard6430(1, 0x6430, szBuf, board))
                {
                   //
                }
                else
                {
                    //
                }
            }
    When i click the button, i get a trying to write to protected memory exception.
    What am i doing wrong?


    Any help will be GREATLY appreciated.

    Thanks
    Last edited by dvazriel; July 20th, 2009 at 06:50 PM.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Help Porting C/C++ Function to C#

    Are you sure that your types match up, i.e., you are not using an int where the C dll expects a short, something like that? That could cause this, though it may also be the C# struct itself, hard to say. Maybe someone with more pinvoke experience can add more info.

  3. #3
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Help Porting C/C++ Function to C#

    I seem to remember that BOOL is an int type in C++. Unlike C++, C# cannot treat int as bool. I'm looking at the line "if (!OpenBoard6430(...". If that function is returning an int, I'm not quite sure what will happen... If internally C# bool's take up less stack space than C++'s BOOL's, something will get overwritten by the return value.

    Incidentally, if "OpenBoard6430" was a C# function returning an int, the application would not compile.

    Wild guess, maybe try:
    Code:
    if (!Convert.ToBoolean(OpenBoard6430(...
    Last edited by rliq; July 20th, 2009 at 07:58 PM.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  4. #4
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Help Porting C/C++ Function to C#

    where have you compiled the C dll ? is it in microsoft visual studio 6.0 ? i.e, versions less than the .NET supported versions...

  5. #5
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Help Porting C/C++ Function to C#

    BOOL maps to bool in .net

    your config needs to have the "ref" keyword before the type in the method definition and your struct needs to have a structlayout attribute applied to it. remember that C++ int's aren't the same size as C# ints and if you're doing your structure memory layout, if you use the auto layout, your struct will not marshal correctly. you'll have the first 16 bits and the second 16 bits all inside your first int, and the second int will be 0.
    Last edited by MadHatter; July 21st, 2009 at 11:22 AM.

  6. #6
    Join Date
    Feb 2007
    Posts
    28

    Re: Help Porting C/C++ Function to C#

    Thanks for the replies. Someone helped me find the solution:

    Code:
    [DllImport("drvr6430.dll")]
    public static extern bool OpenBoard6430(int num, int id, StringBuilder szBuf, out BoardConfig board);
    Basically it replaces the char[] with a StringBuilder object and adding the out keyword to boardConfig)

    Mad: i am not sure what that struct layout attribute does, can you provide me a quick code example of where that attribute must go? thanks!

  7. #7
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Help Porting C/C++ Function to C#

    Here is the sample piece of code using StructLayout Attribute :

    Code:
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct ParserMacroInfoTree
        {
            public ParserMacroInfo Element;
            public IntPtr next;
            //public IntPtr Right;
        }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured