|
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 !!
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
|
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 !!
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
Thank you very much !! I got it )
|
|
|
|
|
Show us the code where call that function, the error is likely there in the data.
|
|
|
|
|
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 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.
|
|
|
|
|
how to have treeview in asp:menu ?
Thanks in advanced!
|
|
|
|
|
The same like any other control. Did you try anything, or are you just looking for someone to make your treeview?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
|
thanks!
i dont wanna use component !
|
|
|
|
|
I have created a window to simulate the keyboard, how to make the cursor in the program window not focused, so that the simulated keyboard can enter a string into the text box of the specified window?
|
|
|
|
|
You'll need to change your form so that it does not receive input-focus. Google for "WS_EX_NOACTIVATE", or check out the article[^] on creating a virtual keyboard here on CodeProject.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
|
I assume you know that you can use Cursor.Hide(); to hide the Cursor, and Cursor.Show(); to restore it.
Is your window with simulated keyboard on a touch-enabled device ? That's the only scenario I can imagine where you'd want to hide the Cursor.
If I am not understanding your idea here, please say more.
bill
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
|
|
|
|
|
How to create a control from scratch? (for WinForm applications)
NOT inherit/extend UserControl.
I want to create something totally new.
A good example is the timer control properties - i bet that is not an extension from Usercontrol.
Something similar i need.
{
I just want to create(fromScratch),visualize and use a NEW control with its properties (and events).
}
For example:
I want a Control named "Car"
Properties: wheels,doors,windows,lights,sounds,color,name,class,subclass,etc
Events: Speed,Stop, Accelerate,Brake,click, collide,touch, lightsOn,soundOn,noWheels,etc
And nothing more than this !
Thanks.
modified 17-Oct-13 5:08am.
|
|
|
|
|
Does the NoWheels event cause them to drop off, like something out of Wacky Races?
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
when are all over worn/overused, maybe? idontknow.
|
|
|
|
|
any idea on how can be achieved this high level of programming stuff?
Any simple example --rudimentary-- like a snippet ?
What i manage to create until now: But is a UserControl extension.
public partial class Car : UserControl
{
public Car()
{
InitializeComponent();
CarLoad();
}
public void CarLoad()
{
Life = 50;
Wheels = 4;
PaintColor = BackColor = Color.Aqua;
this.Size = new Size(20, 20);
}
[DefaultValue(50)]
[Description("...")]
public int Life { set; get; }
[DefaultValue(4)]
public int Wheels { set; get; }
[DefaultValue(typeof(Color), "Aqua ")]
public Color PaintColor { set; get; }
|
|
|
|
|
What you are describing is an object not a control. A control implies that it is a user interface element whereas an object has a bunch of properties and events.
Never underestimate the power of human stupidity
RAH
|
|
|
|