[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

microsoft.public.dotnet.framework.odbcnet

convert c# string to a byte *

melissa manoog

10/15/2002 10:28:00 PM

I am trying to use C# to open a an ODBC connection,
(my database vendor is Centura SQL Base.)
The ODBC call is straight forward when called from managed
C++, namely it is like this:

SQLHDBC hdbc = SQL_NULL_HDBC;
// connect
retcode = SQLConnect( hdbc, (SQLCHAR*)"odbcname", SQL_NTS,
(SQLCHAR*)"sysadm", SQL_NTS, (SQLCHAR*)"sysadm", SQL_NTS);
if (retcode == SQL_SUCCESS || retcode ==
SQL_SUCCESS_WITH_INFO) { . . . etc...

I C# I am stuck because I do not know what is the datatype
(SQLCHAR*) in C#. Since I Added the reference to this API,
I am getting intelli-sense help and it is telling me that
the expected type is byte * (pointer to byte). I tried
everything I know to convert a string such as "sysadm"
into a pointer to byte, and I always get a compile error.
If anyone knows how to convert a string to a byte pointer -
it will be greatly appreciated.


2 Answers

=?iso-8859-1?Q?Andr=E9_Malafaya_Baptista?=

10/23/2002 3:36:00 AM

0

The System.Runtime.InteropServices.Marshal class has lots=20
of conversion functions between managed and unmanaged=20
types. In your particular case, I believe you wish to=20
convert a String to an ANSI unmanaged string (aka char*)=20
which you can use later as a byte*.
In order to do this, you must mark this portion of code=20
as unsafe, pin your String and then call PtrToStringAnsi
(...) static method of the Marshall class.
I don't recall the method's syntax exactly, but first you=20
need to extract the pointer to your String (ToPointer()?)=20
and use it as a parameter for the PtrToStringAnsi method.

HTH
Andr=E9

>-----Original Message-----
>I am trying to use C# to open a an ODBC connection,
>(my database vendor is Centura SQL Base.)
>The ODBC call is straight forward when called from=20
managed=20
>C++, namely it is like this:
>
>SQLHDBC hdbc =3D SQL_NULL_HDBC; =09
>// connect
>retcode =3D SQLConnect( hdbc, (SQLCHAR*)"odbcname",=20
SQL_NTS,=20
>(SQLCHAR*)"sysadm", SQL_NTS, (SQLCHAR*)"sysadm",=20
SQL_NTS);
>if (retcode =3D=3D SQL_SUCCESS || retcode =3D=3D=20
>SQL_SUCCESS_WITH_INFO) { . . . etc...
>
>I C# I am stuck because I do not know what is the=20
datatype
>(SQLCHAR*) in C#. Since I Added the reference to this=20
API,
>I am getting intelli-sense help and it is telling me=20
that=20
>the expected type is byte * (pointer to byte). I tried=20
>everything I know to convert a string such as "sysadm"=20
>into a pointer to byte, and I always get a compile error.
>If anyone knows how to convert a string to a byte=20
pointer -
> it will be greatly appreciated.
>
>=20
>.
>

=?iso-8859-1?Q?Andr=E9_Malafaya_Baptista?=

10/23/2002 3:45:00 AM

0

I'm sorry.
I just realised I made a mistake.
The method is StringToHGlobalAnsi and not PtrToStringAnsi=20
(which does the opoosite).
You should pass your String as a parameter and you get an=20
IntPtr pointing to a newly allocated buffer (in the=20
unmanaged heap) which you can safely convert to a=20
char*/byte* (i.e., (byte*)intptr.ToInt32(NULL) ).
You must not forget to release the buffer when you're=20
finished using it (with FreeHGlobal).

HTH
Andr=E9

>-----Original Message-----
>I am trying to use C# to open a an ODBC connection,
>(my database vendor is Centura SQL Base.)
>The ODBC call is straight forward when called from=20
managed=20
>C++, namely it is like this:
>
>SQLHDBC hdbc =3D SQL_NULL_HDBC; =09
>// connect
>retcode =3D SQLConnect( hdbc, (SQLCHAR*)"odbcname",=20
SQL_NTS,=20
>(SQLCHAR*)"sysadm", SQL_NTS, (SQLCHAR*)"sysadm",=20
SQL_NTS);
>if (retcode =3D=3D SQL_SUCCESS || retcode =3D=3D=20
>SQL_SUCCESS_WITH_INFO) { . . . etc...
>
>I C# I am stuck because I do not know what is the=20
datatype
>(SQLCHAR*) in C#. Since I Added the reference to this=20
API,
>I am getting intelli-sense help and it is telling me=20
that=20
>the expected type is byte * (pointer to byte). I tried=20
>everything I know to convert a string such as "sysadm"=20
>into a pointer to byte, and I always get a compile error.
>If anyone knows how to convert a string to a byte=20
pointer -
> it will be greatly appreciated.
>
>=20
>.
>