CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  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.

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