[lnkForumImage]
TotalShareware - Download Free Software

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


 

kris

6/29/2007 11:32:00 AM

Hi ,

I have a problem passing enum as a parameter in my C# code.
I tried declaring the parameter as int as well as long . I even tried using
MarshalAs attribute.
I am getting exception saying parameters are not matching.
This code is talking to a C++ DLL that takes enum as one of the parameters.

Regards
Kris




4 Answers

(Mattias Sjögren)

6/30/2007 8:44:00 PM

0


>I tried declaring the parameter as int as well as long .

Why not the actual enum type?


>I am getting exception saying parameters are not matching.

Can you please post the exact exception type and error message, as
well as the signatures (both in C++ and C#) of the function you're
caling.


Mattias

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

kris

7/2/2007 10:37:00 AM

0

Hi mattias,



I have mapped it to enum , but still it does not work.

Can you please help me.





Please check below the exact signatures for the functions.

Also please check the structures declarations in both C# and c++.

/////////////////////////////////////////////////////////////////////////////////////

//this is in C++

DLL_EXPORT long __stdcall Connect(char *pServer, long port, XS_Mode mode);
/////////////////////////////////////////////////////////////////////////////////////



//this is in C#

[DllImport("compt.dll", CharSet = CharSet.Ansi,CallingConvention =
CallingConvention.StdCall)]



public static extern long Connect(string pServer, long port, XMode mode);
/////////////////////////////////////////////////////////////////////////////////////



//this is in C++

typedef enum _X_Mode {



NONE = 1,



SERVER = 2,



LOCAL = 3



} XSMode;



/////////////////////////////////////////////////////////////////////////////////////

//this is in C#

public enum XMode



{



NONE = 1,



SERVER = 2,



LOCAL = 3



};

/////////////////////////////////////////////////////////////////////////////////////

"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:e6LnNe1uHHA.5036@TK2MSFTNGP03.phx.gbl...
>
>>I tried declaring the parameter as int as well as long .
>
> Why not the actual enum type?
>
>
>>I am getting exception saying parameters are not matching.
>
> Can you please post the exact exception type and error message, as
> well as the signatures (both in C++ and C#) of the function you're
> caling.
>
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.org
> http://www.msjogren.n... | http://www.dotneti...
> Please reply only to the newsgroup.


Christian Fröschlin

7/2/2007 1:18:00 PM

0

kris wrote:

> public static extern long Connect(string pServer, long port, XMode mode);

C# "long" ist 64-bit, so you should use int for port and return value.

kris

7/4/2007 4:15:00 AM

0

Hi Chris,

Thanks a lot, it works.

Regards
Kris


"Christian Fröschlin" <froeschlin@mvtec.com> wrote in message
news:f6attu$n0s$1@svr7.m-online.net...
> kris wrote:
>
>> public static extern long Connect(string pServer, long port, XMode mode);
>
> C# "long" ist 64-bit, so you should use int for port and return value.