[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

AccessViolationException with simple test P/Invoke

Mesan

10/17/2007 11:27:00 PM

I'm trying to acces some C logic through .Net and am consistently
getting an AccessViolationException. I watered down everything to the
most basic level I could and I'm still getting it. Here's the C code:

void __declspec(dllexport) getText(int num, char* date_text)
{
sprintf(date_text, "Number: %ld", num);
}

Here's the .Net Code:

[DllImport(@"c:\mycode.exe", EntryPoint="getText",
CharSet=CharSet.Ansi)]
static extern void getText(int num, out StringBuilder answer);

StringBuilder sb = new StringBuilder(50);
getText(1234, out sb); <-- When that line runs an
AccessViolationException is thrown.

What am I doing wrong? Instead of using "out StringBuilder" I've
tried "ref", I've tried just sending the StringBuilder, I've tried
adding a "[MarshalAs(UnmanagedType.LPStr)]" attribute to the parameter
definition - nothing helps.

What am I missing?

7 Answers

(Mattias Sjögren)

10/18/2007 5:06:00 AM

0


>What am I doing wrong? Instead of using "out StringBuilder" I've
>tried "ref", I've tried just sending the StringBuilder, I've tried
>adding a "[MarshalAs(UnmanagedType.LPStr)]" attribute to the parameter
>definition - nothing helps.
>
>What am I missing?

The StringBuilder should be passed in by value, no ref/out. You may
also need to set the calling convention to cdecl in the DllImport
attribute.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.n... | http://www.dotneti...
Please reply only to the newsgroup.

Mesan

10/18/2007 4:52:00 PM

0

On Oct 17, 10:05 pm, Mattias Sj?gren <mattias.dont.want.s...@mvps.org>
wrote:
> >What am I doing wrong? Instead of using "out StringBuilder" I've
> >tried "ref", I've tried just sending the StringBuilder, I've tried
> >adding a "[MarshalAs(UnmanagedType.LPStr)]" attribute to the parameter
> >definition - nothing helps.
>
> >What am I missing?
>
> The StringBuilder should be passed in by value, no ref/out. You may
> also need to set the calling convention to cdecl in the DllImport
> attribute.
>
> Mattias
>
> --
> Mattias Sj?gren [C# MVP] mattias @ mvps.orghttp://www.msjogren.n...|http://www.dotneti...
> Please reply only to the newsgroup.



I tried (again) passing the StringBuilder by value (no ref/out). I
also tried (again) using CallingConvention.Cdecl...same results.
Everything I read says this should work no problem. :(

What else should I be looking for?

Thanks!

Phil Wilson

10/19/2007 10:20:00 PM

0

Are you really doing a DllImport from a Win32 .exe??? That won't work.
--
Phil Wilson
[Microsoft MVP Windows Installer]
"Mesan" <935main@gmail.com> wrote in message
news:1192726303.877052.285420@t8g2000prg.googlegroups.com...
On Oct 17, 10:05 pm, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:
> >What am I doing wrong? Instead of using "out StringBuilder" I've
> >tried "ref", I've tried just sending the StringBuilder, I've tried
> >adding a "[MarshalAs(UnmanagedType.LPStr)]" attribute to the parameter
> >definition - nothing helps.
>
> >What am I missing?
>
> The StringBuilder should be passed in by value, no ref/out. You may
> also need to set the calling convention to cdecl in the DllImport
> attribute.
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP] mattias @
> mvps.orghttp://www.msjogren.n...|http://www.dotneti...
> Please reply only to the newsgroup.



I tried (again) passing the StringBuilder by value (no ref/out). I
also tried (again) using CallingConvention.Cdecl...same results.
Everything I read says this should work no problem. :(

What else should I be looking for?

Thanks!


Mesan

10/22/2007 4:14:00 PM

0

On Oct 19, 3:20 pm, "Phil Wilson"
<phil.wil...@wonderware.something.com> wrote:
> Are you really doing a DllImport from a Win32 .exe??? That won't work.
> --
> Phil Wilson
> [Microsoft MVP Windows Installer]"Mesan" <935m...@gmail.com> wrote in message
>
> news:1192726303.877052.285420@t8g2000prg.googlegroups.com...
> On Oct 17, 10:05 pm, Mattias Sj?gren <mattias.dont.want.s...@mvps.org>
> wrote:
>
>
>
>
>
> > >What am I doing wrong? Instead of using "out StringBuilder" I've
> > >tried "ref", I've tried just sending the StringBuilder, I've tried
> > >adding a "[MarshalAs(UnmanagedType.LPStr)]" attribute to the parameter
> > >definition - nothing helps.
>
> > >What am I missing?
>
> > The StringBuilder should be passed in by value, no ref/out. You may
> > also need to set the calling convention to cdecl in the DllImport
> > attribute.
>
> > Mattias
>
> > --
> > Mattias Sj?gren [C# MVP] mattias @
> > mvps.orghttp://www.msjogren.n...|http://www.dotneti...
> > Please reply only to the newsgroup.
>
> I tried (again) passing the StringBuilder by value (no ref/out). I
> also tried (again) using CallingConvention.Cdecl...same results.
> Everything I read says this should work no problem. :(
>
> What else should I be looking for?
>
> Thanks!- Hide quoted text -
>
> - Show quoted text -

It worked for my sum(int a, int b) function. Why wouldn't it work?

Mesan

10/22/2007 5:50:00 PM

0

On Oct 19, 3:20 pm, "Phil Wilson"
<phil.wil...@wonderware.something.com> wrote:
> Are you really doing a DllImport from a Win32 .exe??? That won't work.
> --
> Phil Wilson
> [Microsoft MVP Windows Installer]"Mesan" <935m...@gmail.com> wrote in message
>
> news:1192726303.877052.285420@t8g2000prg.googlegroups.com...
> On Oct 17, 10:05 pm, Mattias Sj?gren <mattias.dont.want.s...@mvps.org>
> wrote:
>
>
>
>
>
> > >What am I doing wrong? Instead of using "out StringBuilder" I've
> > >tried "ref", I've tried just sending the StringBuilder, I've tried
> > >adding a "[MarshalAs(UnmanagedType.LPStr)]" attribute to the parameter
> > >definition - nothing helps.
>
> > >What am I missing?
>
> > The StringBuilder should be passed in by value, no ref/out. You may
> > also need to set the calling convention to cdecl in the DllImport
> > attribute.
>
> > Mattias
>
> > --
> > Mattias Sj?gren [C# MVP] mattias @
> > mvps.orghttp://www.msjogren.n...|http://www.dotneti...
> > Please reply only to the newsgroup.
>
> I tried (again) passing the StringBuilder by value (no ref/out). I
> also tried (again) using CallingConvention.Cdecl...same results.
> Everything I read says this should work no problem. :(
>
> What else should I be looking for?
>
> Thanks!- Hide quoted text -
>
> - Show quoted text -

I re-compiled the code as a DLL and am getting the exact same error,
if you're curious.
I also tried another test function:

void __declspec(dllexport) testInt(int *outInt)
{
outInt++;
}

then in .Net:

[DllImport(@"c:\mydll.dll",
EntryPoint = "testInt",
ExactSpelling = false,
CallingConvention = CallingConvention.Cdecl)]
static extern void testMyInt(ref int testInt);

used as:

int testInt = 5;
testMyInt(ref testInt); <-- That line still throws an Access
Violation Exception

Singrish King

10/24/2010 5:32:00 PM

0

<Singlish> why not make it like "kelong": fish very easy to swim
inside, but blur blur dunno how to swim out? Gorge Solo himself oso got
say: exacly becos garmen no law against hot money, deswhy he speculade
speculade speculade until one day garmen change law!

<Cantonese> ???????????????????????? ????????????* ?????????:????????????????????????????????????
?????????????????????
???????????????????????????????????????????????????????????????????????????????????????????????????????????
???????????????????????????
????????????????????????!

* ??????:????????? /kelong/?????????????????????????????????



On 25/10/10 00:19, ltlee1@hotmail.com wrote:
> On Oct 24, 11:01 am, Singrish King<Singrish.K...@Singkapoor.conned>
> wrote:
>> On 24/10/2010 9:13 PM, ltl...@hotmail.com wrote:
>> ...> Question:
>>> If a country increases its productivity through hardworking and its
>>> people's ingenuity, its currency will sooner or later appreciate. When
>>> it does appreciate, the people, as a whole, would be rewarded for
>>> their collective effort. That is fair. How about the influx of
>>> speculative capitals aiming to reap the benefit and in the process
>>> disrupt the normal operation of the economy?
>>
>> ...
>>
>> <Singlish> why George Sorrows money masuk China, not count as support
>> Chinese e-CON-no-me laeh?
>>
>> <Cantonese> ??????????????????????????????????????????????????????????????????????????????

Singrish King

10/24/2010 5:46:00 PM

0

<Singlish> btw hor, can "blur water catch fish" a-not?

<Cantonese> ???????????????????????????????????????????????????




On 25/10/10 01:31, Singrish King wrote:
> <Singlish> why not make it like "kelong": fish very easy to swim
> inside, but blur blur dunno how to swim out? Gorge Solo himself oso got
> say: exacly becos garmen no law against hot money, deswhy he speculade
> speculade speculade until one day garmen change law!
>
> <Cantonese> ???????????????????????? ????????????* ?????????:????????????????????????????????????
> ?????????????????????
> ???????????????????????????????????????????????????????????????????????????????????????????????????????????
> ???????????????????????????
> ????????????????????????!
>
> * ??????:????????? /kelong/?????????????????????????????????