RV: Convert C++ to C# OpenSSL app

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

RV: Convert C++ to C# OpenSSL app

Daniel Diaz Sanchez-2

Hi,


There is no problem to use your code with C#. What you need to do is to
create a wrapper. First create a dll (a confortable way to do that) with the
functions you need to use from C# ( in your case, rename tmain to for
example TLS_whatever) and then user platform invocation to access from C#.

An example  that uses two functions implemented in two different C++ dlls:

Functions in C/C++

DWORD dwStart = GetTickCount();

LONG SCardEstablishContext(
  DWORD dwScope,
  LPCVOID pvReserved1,
  LPCVOID pvReserved2,
  LPSCARDCONTEXT phContext
);

Note that the types used in C/C++ should be converted to others equivalents
in C# (if it is a pointer, use IntPtr)

Find more information searching pInvoke or Platform Invocation in MSDN


using System;

using System.Runtime.InteropServices;

namespace YourNameSpace
{
        /// <summary>
        /// Doc.
        /// </summary>
       
        public class mywrapper
        {
         // this is optional: it disables heap walks security checking and
improves performance
                [System.Security.SuppressUnmanagedCodeSecurityAttribute()]

         [DllImport("Kernel32.dll")]
                public unsafe static extern int GetTickCount();

                //note that here we are not suppressing security checking

                [DllImport("winscard.dll")]
                public unsafe static extern Int32 SCardEstablishContext(
UInt32 Scope, IntPtr pReserved1, IntPtr pReserserved2, UInt32 *phContext);
        }

        class Class1
        {
                /// <summary>
                /// </summary>
                [STAThread]
                static void Main(string[] args)
                {
                        UInt32 hContext = 0;
                        int Result = 0;
                        UInt32 SCARD_SCOPE_USER = 0;
                       
                        // if you are not using pointers (as we were not
using C/C++)
         mywrapper.GetTickCount();

                        unsafe // if you use pointers to behave like in
C/C++ note the parameter &hContext
                        {
                                Result = mywrapper.SCardEstablishContext(
SCARD_SCOPE_USER, a , a, &hContext);
                                if ( Result != 0 )
                                        Console.WriteLine("Error stablish
context");
                               
    }
                }

         }
}


Daniel Díaz Sánchez

-----Mensaje original-----
De: [hidden email] [mailto:[hidden email]]
En nombre de Hashim Saleem
Enviado el: martes, 24 de enero de 2006 13:45
Para: [hidden email]
Asunto: Re: Convert C++ to C# OpenSSL app

One other option is to write a managed wrapper in visual C++ .net using the
IJW (It just works) technology. This way, the resulting wrapper (.net
assembly) could be called from any .net supported language e.g. C#, VB.net,
J# etc.

I can provide an example of this if it is required.

Regards,
Hashim Saleem

----- Original Message -----
From: "Thomas J. Hruska" <[hidden email]>
To: <[hidden email]>
Sent: Sunday, January 22, 2006 8:34 AM
Subject: Re: Convert C++ to C# OpenSSL app


> OpenSSLGRT wrote:
>> I just got Mr. Sanchez OpenSSL port to PDA
>> (http://karajan.it.uc3m.es/~pervasive/wce_lite_compat/) - and it works
>> great! I have a simple SSL Client in C++. Now I want to convert that to
>> C#.
>> I assume(???) that I would use the two DLLs but libeay32.dll and
>> ssleay32.dll that I now have on the PDA. I know how to use simple dlls
>> from
>> C++ in C# but I am having trouble figuring out how to convert this more
>> complicated program.  Any help would be greatly!!! Appretiated
>>
>>  A simple app (which I got a lot from a great source on the web!) in C++
>> posts vars to a server and works fine - now to get it into C# (I can get
>> the
>> C++ generic code translated OK it is just how do I get at the OpenSSL
>> stuff
>> like for example:  SSL_load_error_strings();
>
> Look at how PayPal interacts with OpenSSL.  They build an intermediate
> unmanaged DLL with C++ that does the actual communication with OpenSSL and

> then call the intermediate DLL from C#.  Source code for this is available

> in the PayPal SDK.
>
> --
> Thomas Hruska
> Shining Light Productions
>
> Home of BMP2AVI, Nuclear Vision, ProtoNova, and Win32 OpenSSL.
> http://www.slproweb.com/
>
> Ask me about discounts on any Shining Light Productions product!
>
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    [hidden email]
> Automated List Manager                           [hidden email]
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]

smime.p7s (3K) Download Attachment