Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2003

Tip: Looking for answers? Try searching our database.

ExecutionEngineException when using a DLLImport call

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Magnus Ullberg - 14 Sep 2003 01:47 GMT
How do i convert the following structure declaration from c++ to c#?

#pragma pack(push,1) //Byte alignment
struct TtsrPlayerInfo
{
int PlayerID;
int ChannelID;
char NickName[30];
int PlayerChannelPrivileges;
int PlayerPrivileges;
int PlayerFlags;
};
#pragma pack(pop)

This is the dllimport statement looks like
[DllImport("tsremote.dll")]
public static extern int tsrGetPlayers(out TtsrPlayerInfo[] tsrPlayers, out
int PlayerRecords);

This is what i though the correct declaration would look like:
public struct TtsrPlayerInfo
{
public int PlayerID;
public int ChannelID;
public char[] NickName;
public int PlayerChannelPrivileges;
public int PlayerPrivileges;
public int PlayerFlags;
};

I've tried adding the following line just above the struct but it doesnt
seem to help. When the code runs i get a ExecutionEngineException.
[StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, Pack =
1)]

What i'm trying to do is to get data from an external DLL imported using
DLLImport. I can get data from other dll calls just fine as long as the data
structure doesn't contain a char[].

Thanks,
Magnus Ullberg
Mattias Sj?gren - 14 Sep 2003 16:53 GMT
Magnus,

>How do i convert the following structure declaration from c++ to c#?

[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct TtsrPlayerInfo
{
 public int PlayerID;
 public int ChannelID;
 [MarshalAs(UnmanagedType.ByValArray, SizeConst=30)]
 public char[] NickName;
 // or
 // [MarshalAs(UnmanagedType.ByValTStr, SizeConst=30)]
 // public string NickName;
 public int PlayerChannelPrivileges;
 public int PlayerPrivileges;
 public int PlayerFlags;
}

>This is the dllimport statement looks like
>[DllImport("tsremote.dll")]
>public static extern int tsrGetPlayers(out TtsrPlayerInfo[] tsrPlayers, out
>int PlayerRecords);

What's the unmanaged function signature? Are you sure that the first
parameter should be out?

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Magnus Ullberg - 15 Sep 2003 14:11 GMT
> Magnus,
>
[quoted text clipped - 14 lines]
>   public int PlayerFlags;
> }

i'll give that a shot, thanks

> >This is the dllimport statement looks like
> >[DllImport("tsremote.dll")]
[quoted text clipped - 3 lines]
> What's the unmanaged function signature? Are you sure that the first
> parameter should be out?

Here is the definition from some c++ code that works fine:

//##########################################################################
####

//#

//# Function int tsrGetPlayers(TtsrPlayerInfo *tsrPlayers,

//# int *PlayerRecords);

//#

//# Description:

//# Get a list of the Players on the server.

//#

//# Input:

//# tsrPlayers: A pointer to an array of TtsrPlayerInfo records

//# PlayerRecords: pointer to a integer which specifies how many

//# TtsrPlayerInfo records tsrPlayers can hold.

//#

//# Output:

//# Result: 0 = OK, else the error number

//# if result = 0 then tsrPlayers is filled with the Player info.

//# PlayerRecords will have the number or records that were filled

//#

//##########################################################################
####

typedef int (__stdcall *LPtsrGetPlayers)(TtsrPlayerInfo *tsrPlayers,

int *PlayerRecords);

LPtsrGetPlayers tsrGetPlayers = NULL;

Later on in the code this runs to initialize it

tsrGetPlayers = (LPtsrGetPlayers)GetProcAddress(tsrDLL,"tsrGetPlayers");
Magnus Ullberg - 15 Sep 2003 14:33 GMT
> Magnus,
>
[quoted text clipped - 14 lines]
>   public int PlayerFlags;
> }

It still gives me the same error.

Here is some c++ code that works fine. I believe that it also works without
using a pointer, but im not sure.

TtsrPlayerInfo* CurrentPlayers;
int CurrentPlayerCount = MAX_PLAYERS;
CurrentPlayers = new TtsrPlayerInfo[MAX_PLAYERS];
int retval = tsrGetPlayers(CurrentPlayers, &CurrentPlayerCount);
Ying-Shen Yu[MSFT] - 16 Sep 2003 07:35 GMT
Hi Magnus,
  I think you should not use the c# keyword out here, out keyword
indicates an parameter is being transferred by reference, which is not
applicable here.
This is the definition of your type,
//from Mattias
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct TtsrPlayerInfo
{
    public int PlayerID;
    public int ChannelID;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=30)]
    public char[] NickName;
    // or
    //[MarshalAs(UnmanagedType.ByValTStr, SizeConst=30)]
    //public string NickName;
    public int PlayerChannelPrivileges;
    public int PlayerPrivileges;
    public int PlayerFlags;
}
class PInvoke
{
    [DllImport("test.dll")]
    public extern static int tsrGetPlayers([In,Out] TtsrPlayerInfo[]
tsrPlayers, ref int PlayerRecords);
}

your unmanaged  function definition
int tsrGetPlayers(TtsrPlayerInfo *tsrPlayers,int *PlayerRecords);

You need create your array in managed code before passing it into your
function.
Here is a snippet from my test code.
<code>
int recs = 12;
TtsrPlayerInfo[] info = new TtsrPlayerInfo[recs];
Console.WriteLine(PInvoke.tsrGetPlayers(info,ref recs));
Console.WriteLine(info.Length);
foreach(TtsrPlayerInfo i in info)
{
    Console.WriteLine(i.ChannelID);
    Console.WriteLine(i.NickName);
    Console.WriteLine(i.PlayerChannelPrivileges);
    Console.WriteLine(i.PlayerFlags);
    Console.WriteLine(i.PlayerID);
    Console.WriteLine(i.PlayerPrivileges);
}
Console.Read();
</code>

Thanks, if you still have problems on it ,please let me know!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" shouldbe removed before
sending, Thanks!

--------------------
| Reply-To: "Magnus Ullberg" <magnus@areaconsulting.us>
| From: "Magnus Ullberg" <magnus@areaconsulting.us>
| References: <#urrtoleDHA.2264@TK2MSFTNGP12.phx.gbl>
<uBGpHiteDHA.556@TK2MSFTNGP11.phx.gbl>
| Subject: Re: ExecutionEngineException when using a DLLImport call
| Date: Mon, 15 Sep 2003 08:33:05 -0500
[quoted text clipped - 8 lines]
| NNTP-Posting-Host: ky-owensboro1b-121.owboky.adelphia.net 24.52.178.121
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.interop:18727
| X-Tomcat-NG: microsoft.public.dotnet.framework.interop
[quoted text clipped - 27 lines]
| CurrentPlayers = new TtsrPlayerInfo[MAX_PLAYERS];
| int retval = tsrGetPlayers(CurrentPlayers, &CurrentPlayerCount);
Magnus Ullberg - 16 Sep 2003 14:08 GMT
Great, thanks a lot.
That works prefectly.

Do you have any suggestions on where to find more info about the [in,out]
declaration? I've never seen that before, it that basically a pointer?

Thanks,
Magnus Ullberg
Area Consulting Group, Inc.

> Hi Magnus,
>    I think you should not use the c# keyword out here, out keyword
[quoted text clipped - 75 lines]
> | NNTP-Posting-Host: ky-owensboro1b-121.owboky.adelphia.net 24.52.178.121
> | Path:

cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
> phx.gbl!tk2msftngp13.phx.gbl
> | Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.interop:18727
[quoted text clipped - 29 lines]
> | CurrentPlayers = new TtsrPlayerInfo[MAX_PLAYERS];
> | int retval = tsrGetPlayers(CurrentPlayers, &CurrentPlayerCount);
Ying-Shen Yu[MSFT] - 16 Sep 2003 17:25 GMT
Hi Magnus,
It's my pleasure to help you solve your problem,
Thanks for using MSDN Newsgroup!
In, Out is Directional Attribute in CLR.
If you want to find more info about the these two attributes
you may search InAttribute and OutAttribute in your MSDN Index,
Or here is link
<InAttribute>
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemRuntimeInterop
ServicesInAttributeClassTopic.asp?frame=true
<OutAttribute>
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemRuntimeInterop
ServicesOutAttributeClassTopic.asp?frame=true

And there are also some examples on Interop comes with .NET Framework SDK
by default, they should be at,
\Program
Files\Microsoft.NET\SDK\v1.1\Samples\Technologies\Interop\PlatformInvoke\Cus
tom

Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" shouldbe removed before
sending, Thanks!

--------------------
| Reply-To: "Magnus Ullberg" <magnus@areaconsulting.us>
| From: "Magnus Ullberg" <magnus@areaconsulting.us>
| References: <#urrtoleDHA.2264@TK2MSFTNGP12.phx.gbl>
<uBGpHiteDHA.556@TK2MSFTNGP11.phx.gbl>
<e5laF54eDHA.576@tk2msftngp13.phx.gbl>
<8CD5syBfDHA.536@cpmsftngxa07.phx.gbl>
| Subject: Re: ExecutionEngineException when using a DLLImport call
| Date: Tue, 16 Sep 2003 08:08:53 -0500
[quoted text clipped - 8 lines]
| NNTP-Posting-Host: ky-owensboro1b-121.owboky.adelphia.net 24.52.178.121
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.interop:18753
| X-Tomcat-NG: microsoft.public.dotnet.framework.interop
[quoted text clipped - 88 lines]
| > | NNTP-Posting-Host: ky-owensboro1b-121.owboky.adelphia.net 24.52.178.121
| > | Path:

cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
| > phx.gbl!tk2msftngp13.phx.gbl
| > | Xref: cpmsftngxa07.phx.gbl
[quoted text clipped - 30 lines]
| > | CurrentPlayers = new TtsrPlayerInfo[MAX_PLAYERS];
| > | int retval = tsrGetPlayers(CurrentPlayers, &CurrentPlayerCount);
Magnus Ullberg - 16 Sep 2003 20:19 GMT
> Hi Magnus,
> It's my pleasure to help you solve your problem,
> Thanks for using MSDN Newsgroup!

thanks for all the help

> If you want to find more info about the these two attributes
> you may search InAttribute and OutAttribute in your MSDN Index,

great, exactly what i was looking for

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.