Hi.

I am new to C# and a little stuck on arguments to pass to an external function.

The intelisense tells me the function has these parameters

void LPP.XRS_Search(int x, int y, int w, int z, int cl, int vr, int sp, int[] PointResult)

here is my call

int[] pxscoords = new int[2];
LPP.XRS_Search(0, 0, 1024, 768, 16777215, 0, 1, pxscoords);

here is my error

Quote:
An unhandled exception of type 'System.AccessViolationException' occurred in csharpsearchtest.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


I suspect that the last parameter should be a pointer to an array, but Im new so I do not really know.

I would appreciate some seasoned advice if possible.

Hi.

I am new to C# and a little stuck on arguments to pass to an external function.

The intelisense tells me the function has these parameters

void LPP.XRS_Search(int x, int y, int w, int z, int cl, int vr, int sp, int[] PointResult)

here is my call

int[] pxscoords = new int[2];
LPP.XRS_Search(0, 0, 1024, 768, 16777215, 0, 1, pxscoords);

here is my error

Quote:
An unhandled exception of type 'System.AccessViolationException' occurred in csharpsearchtest.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


I suspect that the last parameter should be a pointer to an array, but Im new so I do not really know.

I would appreciate some seasoned advice if possible.

From what I see, you're passing an empty array to the method call.

In other words, you create the array, but you don't set any values before you pass it as your argument. And because you've only defined the elements, but not set them to any value ... your guess is as good as mine, what values you're actually passing (if you aren't passing nulls).

The function is in an external dll, with the function being void and the varname "PointResult", I'm supposing that is where the results will be held.

I am expecting an x and y coordinate from the function so a 2 element array seems apt, although I tried more elements.

So I guess I'm looking for a way to get a pointer to that array, but from the tutorials I have read so far C# does not use pointers.

The function is in an external dll, with the function being void and the varname "PointResult", I'm supposing that is where the results will be held.

I am expecting an x and y coordinate from the function so a 2 element array seems apt, although I tried more elements.

So I guess I'm looking for a way to get a pointer to that array, but from the tutorials I have read so far C# does not use pointers.

Will 'ref' work here?

Example: LPP.XRS_Search(0, 0, 1024, 768, 16777215, 0, 1, ref pxscoords);

Its a shot in the dark from my side, not knowing .dll.

Thank you for the suggestion I appreciate it.

Unfortunately it does not work, there are error warnings about invalid arguments.

Upon further reading, I see that using pointers has something to do with Unsafe code (but I cannot figure out what this is)

One msdn article says "compile as unsafe" but Its beyond me what it actually means, and dosent sound that inviting.

gtk, in c#, arrays and objects are passed to a method by there reference, so no ref is needed.
About the error, the additional information is the inner exception? Can you post your stack trace?

I am a little embarrassed now, because I don't know what a stack trace is.
Although I will try my best to read it up, if it will help me realize what I'm doing wrong.

Thanks.

Run your application in debug mode (F5) and at a point it will crach. The line with the error is marked with yellow and a dialog box appears with the error. In the " actions" section , there is a link saying "view details". Click on it and another box will appear. Extend the error and there you find inner exception and stack trace.
Stack trace shows you the stack of method calls that a program made to reach the point where it crashed(in this case).

Ionut

Thank you for the reply and information on stack trace, I will no doubt need it.
Regarding my problem, it was hurting my brain and decided that coding my own function would be easier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.