Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. function call from an c++ dll in c#

function call from an c++ dll in c#

Scheduled Pinned Locked Moved C#
helpcsharpc++debuggingquestion
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Willow2008
    wrote on last edited by
    #1

    Hello, I hope somebody can help me and I hope I put my questions into the right forum !! If not pls let me know !! I received a c++ dll with a function inside: int FunctionCall(int* Size, unsigned char Data[], int time) I used the DLLImport:

    [DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)]
    extern static int FunctionCall(IntPtr Size, byte[] data, int timeout);

    But when I debug I always get an out of range error !! What did I wrong ?? Thank you very much for your help !!

    L B OriginalGriffO F 4 Replies Last reply
    0
    • W Willow2008

      Hello, I hope somebody can help me and I hope I put my questions into the right forum !! If not pls let me know !! I received a c++ dll with a function inside: int FunctionCall(int* Size, unsigned char Data[], int time) I used the DLLImport:

      [DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)]
      extern static int FunctionCall(IntPtr Size, byte[] data, int timeout);

      But when I debug I always get an out of range error !! What did I wrong ?? Thank you very much for your help !!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You need to show the code that gives the error, the exact error code or message, and the values of all the variables that are used in the call to the C++ function.

      Veni, vidi, abiit domum

      W 1 Reply Last reply
      0
      • W Willow2008

        Hello, I hope somebody can help me and I hope I put my questions into the right forum !! If not pls let me know !! I received a c++ dll with a function inside: int FunctionCall(int* Size, unsigned char Data[], int time) I used the DLLImport:

        [DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)]
        extern static int FunctionCall(IntPtr Size, byte[] data, int timeout);

        But when I debug I always get an out of range error !! What did I wrong ?? Thank you very much for your help !!

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #3

        Show us the code where call that function, the error is likely there in the data.

        1 Reply Last reply
        0
        • W Willow2008

          Hello, I hope somebody can help me and I hope I put my questions into the right forum !! If not pls let me know !! I received a c++ dll with a function inside: int FunctionCall(int* Size, unsigned char Data[], int time) I used the DLLImport:

          [DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)]
          extern static int FunctionCall(IntPtr Size, byte[] data, int timeout);

          But when I debug I always get an out of range error !! What did I wrong ?? Thank you very much for your help !!

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          As Richard says, we need to see the code you use to all it to be sure, but I think you need to replace the IntPtr with a ref int unless you are already dealing with unsafe code.

          [DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)]
          extern static int FunctionCall(ref int Size, byte[] data, int timeout);

          The only instant messaging I do involves my middle finger. English doesn't borrow from other languages. English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • W Willow2008

            Hello, I hope somebody can help me and I hope I put my questions into the right forum !! If not pls let me know !! I received a c++ dll with a function inside: int FunctionCall(int* Size, unsigned char Data[], int time) I used the DLLImport:

            [DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)]
            extern static int FunctionCall(IntPtr Size, byte[] data, int timeout);

            But when I debug I always get an out of range error !! What did I wrong ?? Thank you very much for your help !!

            F Offline
            F Offline
            Freak30
            wrote on last edited by
            #5

            I think the problem is that byte[] in C# is a managed array, which is not equivalent to the address of the first member as it is in unmanaged C++. You will probably have to use a pointer type for this parameter as well. You will need an unsafe block and should probably also use the fixed keyword to prevent the Garbage Collector from moving the content of the array while the C++ function is using it.

            The good thing about pessimism is, that you are always either right or pleasently surprised.

            1 Reply Last reply
            0
            • L Lost User

              You need to show the code that gives the error, the exact error code or message, and the values of all the variables that are used in the call to the C++ function.

              Veni, vidi, abiit domum

              W Offline
              W Offline
              Willow2008
              wrote on last edited by
              #6

              using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Text;
              using System.Threading.Tasks;
              using System.IO;
              using System.Runtime.InteropServices;
              using System.Data;

              namespace ConsoleApplication1
              {
              class Program
              {
              [DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)]
              extern static int Fct1();

                  \[DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)\]
                  extern static int Fct2();
              
                  \[DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)\]
                  extern static int Fct3(IntPtr Size, byte\[\] data, int timeout);
              
                  \[DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)\]
                  extern static int Fct4(int size, byte\[\] data);
              
                  static void Main(string\[\] args)
                  {
                      byte\[\] data = new byte\[5\] {0x01,0x02,0x03,0x04,0x05};
              
              
                      int result = Fct1();
                      Console.WriteLine(result);
              
                      result = Fct2((IntPtr)2, data, 3);
                      Console.WriteLine(result);
              
                      result = Fct3(3, data);
                      Console.WriteLine(result);
              
                      result = Fct4();
                      Console.WriteLine(result);
              
              
                  }
              }
              

              }

              ErrorMessage: AccessViolation at Fct2

              L _ 2 Replies Last reply
              0
              • W Willow2008

                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;
                using System.Threading.Tasks;
                using System.IO;
                using System.Runtime.InteropServices;
                using System.Data;

                namespace ConsoleApplication1
                {
                class Program
                {
                [DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)]
                extern static int Fct1();

                    \[DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)\]
                    extern static int Fct2();
                
                    \[DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)\]
                    extern static int Fct3(IntPtr Size, byte\[\] data, int timeout);
                
                    \[DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)\]
                    extern static int Fct4(int size, byte\[\] data);
                
                    static void Main(string\[\] args)
                    {
                        byte\[\] data = new byte\[5\] {0x01,0x02,0x03,0x04,0x05};
                
                
                        int result = Fct1();
                        Console.WriteLine(result);
                
                        result = Fct2((IntPtr)2, data, 3);
                        Console.WriteLine(result);
                
                        result = Fct3(3, data);
                        Console.WriteLine(result);
                
                        result = Fct4();
                        Console.WriteLine(result);
                
                
                    }
                }
                

                }

                ErrorMessage: AccessViolation at Fct2

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Your code does not make much sense, I suggest you study http://msdn.microsoft.com/en-us/library/aa288468(VS.71).aspx[^], to understand the P/Invoke rules.

                Veni, vidi, abiit domum

                W 1 Reply Last reply
                0
                • L Lost User

                  Your code does not make much sense, I suggest you study http://msdn.microsoft.com/en-us/library/aa288468(VS.71).aspx[^], to understand the P/Invoke rules.

                  Veni, vidi, abiit domum

                  W Offline
                  W Offline
                  Willow2008
                  wrote on last edited by
                  #8

                  Sorry to bother you :(( I´m very glad about your help !! And I have to excuse me but I don´t have much experience in C# coding..:( I will give you more details what I want to do: I have a windows programm from and I have to make a communication to this programm. The supplier of the other programm provided me a dll with different functions inside. Now from my understanding the easiest way to setup the communication is to use this dll. So I only wanted to make a short trial if I can call this functions and if the communication can be established ! Fct1, Fct3 and Fct4 are returning a value that shows a good result. The problem is Fct2 where I have to deal with pointer :((( Thanks again !!

                  OriginalGriffO L 2 Replies Last reply
                  0
                  • W Willow2008

                    Sorry to bother you :(( I´m very glad about your help !! And I have to excuse me but I don´t have much experience in C# coding..:( I will give you more details what I want to do: I have a windows programm from and I have to make a communication to this programm. The supplier of the other programm provided me a dll with different functions inside. Now from my understanding the easiest way to setup the communication is to use this dll. So I only wanted to make a short trial if I can call this functions and if the communication can be established ! Fct1, Fct3 and Fct4 are returning a value that shows a good result. The problem is Fct2 where I have to deal with pointer :((( Thanks again !!

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #9

                    Even if your code was good, you cannot pass a pointer to a constant value! The point of passing a pointer is so that the method you call can modify the value in your program. If you could pass a pointer to "the number two" what do you expect to happen when the function tries to change it? As I said, you almost certainly want to pass a ref int instead of an IntPtr as the former actually passes a pointer rather than a value in a "pointer package"! (And C# won't let you call your method as Fct2( ref 2, data) for precisely the reasons above)

                    The only instant messaging I do involves my middle finger. English doesn't borrow from other languages. English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    1 Reply Last reply
                    0
                    • W Willow2008

                      Sorry to bother you :(( I´m very glad about your help !! And I have to excuse me but I don´t have much experience in C# coding..:( I will give you more details what I want to do: I have a windows programm from and I have to make a communication to this programm. The supplier of the other programm provided me a dll with different functions inside. Now from my understanding the easiest way to setup the communication is to use this dll. So I only wanted to make a short trial if I can call this functions and if the communication can be established ! Fct1, Fct3 and Fct4 are returning a value that shows a good result. The problem is Fct2 where I have to deal with pointer :((( Thanks again !!

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      You really need to study that P/Invoke tutorial I gave you the link for. You are not going to learn this just by asking a question here, as there are quite a few rules and restrictions that you need to understand.

                      Veni, vidi, abiit domum

                      1 Reply Last reply
                      0
                      • W Willow2008

                        using System;
                        using System.Collections.Generic;
                        using System.Linq;
                        using System.Text;
                        using System.Threading.Tasks;
                        using System.IO;
                        using System.Runtime.InteropServices;
                        using System.Data;

                        namespace ConsoleApplication1
                        {
                        class Program
                        {
                        [DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)]
                        extern static int Fct1();

                            \[DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)\]
                            extern static int Fct2();
                        
                            \[DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)\]
                            extern static int Fct3(IntPtr Size, byte\[\] data, int timeout);
                        
                            \[DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)\]
                            extern static int Fct4(int size, byte\[\] data);
                        
                            static void Main(string\[\] args)
                            {
                                byte\[\] data = new byte\[5\] {0x01,0x02,0x03,0x04,0x05};
                        
                        
                                int result = Fct1();
                                Console.WriteLine(result);
                        
                                result = Fct2((IntPtr)2, data, 3);
                                Console.WriteLine(result);
                        
                                result = Fct3(3, data);
                                Console.WriteLine(result);
                        
                                result = Fct4();
                                Console.WriteLine(result);
                        
                        
                            }
                        }
                        

                        }

                        ErrorMessage: AccessViolation at Fct2

                        _ Offline
                        _ Offline
                        _Erik_
                        wrote on last edited by
                        #11

                        This call is wrong:

                        result = Fct2((IntPtr)2, data, 3);

                        The first parameter should be a pointer to the address of memory which contains the size of the "data" array. Casting a literal "2" to IntPtr is telling the library that the length of the array is in the block of memory with the address 2. Try changing the function definition like this:

                        [DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)]
                        extern static int Fct3(ref int Size, byte[] data, int timeout);

                        And call it like this:

                        int dtLength = data.Length;
                        result = Fct2(ref dtLength, data, 3);

                        See you

                        W 1 Reply Last reply
                        0
                        • _ _Erik_

                          This call is wrong:

                          result = Fct2((IntPtr)2, data, 3);

                          The first parameter should be a pointer to the address of memory which contains the size of the "data" array. Casting a literal "2" to IntPtr is telling the library that the length of the array is in the block of memory with the address 2. Try changing the function definition like this:

                          [DllImport("Function.dll", CallingConvention = CallingConvention.Cdecl)]
                          extern static int Fct3(ref int Size, byte[] data, int timeout);

                          And call it like this:

                          int dtLength = data.Length;
                          result = Fct2(ref dtLength, data, 3);

                          See you

                          W Offline
                          W Offline
                          Willow2008
                          wrote on last edited by
                          #12

                          Thank you very much !! I got it :))

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups