CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2003
    Posts
    46

    How to convert cpp to c?

    I have this cpp code that i need to convert to c

    can anyone help or give advice on converting cpp to c?

    is it possible to convert to c in ms

    header:

    Code:
    class CSSLSocket
    {
    public:
    	CSSLSocket();
    	virtual ~CSSLSocket();
    	bool Init();
    	bool AttachToSocket(int sSocket);
    	int GetSocket();
    	bool Accept();
    	bool Connect();
    	void Close();
    	int Read(void *pBuf, int iNum);
    	int Write(const void *pBuf, int iNum);
    	bool m_bConnected;
    protected:
    	SSL_CTX	*m_psslCtx;
    	SSL		*m_pSSL;
    	int		 m_sSocket;
    };

    cpp code:
    Code:
    CSSLSocket::CSSLSocket()
    {
    	m_psslCtx=NULL;
    	m_pSSL=NULL;
    	SSL_library_init();
    	m_bConnected=FALSE;
    }
    CSSLSocket::~CSSLSocket()
    {	// Cleanup
    	if(m_psslCtx)
    	SSL_CTX_free(m_psslCtx);
    	if(m_pSSL)
    	{
    		SSL_shutdown(m_pSSL);
    		SSL_free(m_pSSL);
    	}
    	m_bConnected=FALSE;
    }
    
    bool CSSLSocket::Init()
    {	
    	m_bConnected=FALSE;
    	// Initialize the context, exit if fails
    	m_psslCtx=SSL_CTX_new(SSLv3_method());	
    	if(!m_psslCtx)
    	return FALSE;
    	// Initialize the object, exit if fails
    	m_pSSL=SSL_new(m_psslCtx);
    	if(!m_pSSL)
    	return FALSE;
    	// Success
    	return TRUE;
    }
    
    bool CSSLSocket::AttachToSocket(int sSocket)
    {
    	m_bConnected=FALSE;
    	// Fail if not initialized
    	if(!m_psslCtx || !m_pSSL)
    	return FALSE;
    	// Store the socket, and attach it to the ssl object
    	m_sSocket=sSocket;
    	if(!SSL_set_fd(m_pSSL, m_sSocket))
    	return FALSE;
    	// Success
    	return TRUE;
    }
    
    int CSSLSocket::GetSocket() {
    return m_sSocket;
    }
    
    bool CSSLSocket::Accept()
    {
    	m_bConnected=FALSE;
    	// Fail if not initialized
    	if(!m_psslCtx || !m_pSSL)
    	return FALSE;
    	if(SSL_accept(m_pSSL)==1)
    	{
    	m_bConnected=TRUE;
    	return TRUE;
    	}
    	else
    	{
    	m_bConnected=FALSE;
    	return FALSE;
    	}
    }
    
    bool CSSLSocket::Connect()
    {	
    	m_bConnected=FALSE;
    	// Fail if not initialized
    	if(!m_psslCtx || !m_pSSL)
    	return FALSE;
    	if(SSL_connect(m_pSSL)==1)
    	{
    	m_bConnected=TRUE;
    	return TRUE;
    	}
    	else
    	{
    	m_bConnected=FALSE;
    	return FALSE;
    	}
    }
    
    void CSSLSocket::Close()
    {
    	SSL_shutdown(m_pSSL);
    	m_bConnected=FALSE;
    }
    
    int CSSLSocket::Read(void *pBuf, int iNum)
    {
    	if(!m_psslCtx || !m_pSSL || !m_bConnected)
    	return 0;
    	return SSL_read(m_pSSL, pBuf, iNum);
    }
    
    int CSSLSocket::Write(const void *pBuf, int iNum)
    {
    	if(!m_psslCtx || !m_pSSL || !m_bConnected)
    	return 0;
    	return SSL_write(m_pSSL, pBuf, iNum);
    }
    Mouse

  2. #2
    Join Date
    Jun 2002
    Posts
    1,417

    Re: How to convert cpp to c?

    create a structure called CSSLSocket and put the data objects into it.
    Code:
    struct CSSLSocket
    {
    	SSL_CTX	*m_psslCtx;
    	SSL		*m_pSSL;
    	int		 m_sSocket;
    };
    now just make the methods of that class global functions that use a pointer to an instance of struct CSSLSocket. There will be no automatic call of the current class constructor and destructor, so just make normal functions out of them and call them explicitly in your code.

  3. #3
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    1,051

    Re: How to convert cpp to c?

    And maybi a few other additional libraries for ...Ex. BOOL .... ( that is if you dont want to modify the existing code and such.
    - Sreehari
    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
    " Everybody is sent to Earth on a purpose. I am so Lagging behind that i won't die." – Calvin

  4. #4
    Join Date
    Jun 2005
    Location
    Tirunelveli-Tamil Nadu-India
    Posts
    354

    Smile Re: How to convert cpp to c?

    use this code

    Code:
    // function decleration
    bool Init();
    bool AttachToSocket(int sSocket);
    int GetSocket();
    bool Accept();
    bool Connect();
    void Close();
    int Read(void *pBuf, int iNum);
    int Write(const void *pBuf, int iNum);
    bool m_bConnected;
    // Variables
    SSL_CTX	*m_psslCtx;
    SSL		*m_pSSL;
    int		 m_sSocket;
    void main()
    {
    	m_psslCtx=NULL;
    	m_pSSL=NULL;
    	SSL_library_init();
    	m_bConnected=FALSE;
    	Init();
    	AttachToSocket(int sSocket);
    	GetSocket();
    	Accept();
    	Connect();
    	Close();
    	Read(void *pBuf, int iNum);
    	Write(const void *pBuf, int iNum);
    }
    bool Init()
    {	
    	m_bConnected=FALSE;
    	// Initialize the context, exit if fails
    	m_psslCtx=SSL_CTX_new(SSLv3_method());	
    	if(!m_psslCtx)
    	return FALSE;
    	// Initialize the object, exit if fails
    	m_pSSL=SSL_new(m_psslCtx);
    	if(!m_pSSL)
    	return FALSE;
    	// Success
    	return TRUE;
    }
    
    bool AttachToSocket(int sSocket)
    {
    	m_bConnected=FALSE;
    	// Fail if not initialized
    	if(!m_psslCtx || !m_pSSL)
    	return FALSE;
    	// Store the socket, and attach it to the ssl object
    	m_sSocket=sSocket;
    	if(!SSL_set_fd(m_pSSL, m_sSocket))
    	return FALSE;
    	// Success
    	return TRUE;
    }
    
    int GetSocket() 
    {
    	return m_sSocket;
    }
    
    bool Accept()
    {
    	m_bConnected=FALSE;
    	// Fail if not initialized
    	if(!m_psslCtx || !m_pSSL)
    	return FALSE;
    	if(SSL_accept(m_pSSL)==1)
    	{
    	m_bConnected=TRUE;
    	return TRUE;
    	}
    	else
    	{
    	m_bConnected=FALSE;
    	return FALSE;
    	}
    }
    
    bool Connect()
    {	
    	m_bConnected=FALSE;
    	// Fail if not initialized
    	if(!m_psslCtx || !m_pSSL)
    	return FALSE;
    	if(SSL_connect(m_pSSL)==1)
    	{
    	m_bConnected=TRUE;
    	return TRUE;
    	}
    	else
    	{
    	m_bConnected=FALSE;
    	return FALSE;
    	}
    }
    
    void Close()
    {
    	SSL_shutdown(m_pSSL);
    	m_bConnected=FALSE;
    }
    
    int Read(void *pBuf, int iNum)
    {
    	if(!m_psslCtx || !m_pSSL || !m_bConnected)
    	return 0;
    	return SSL_read(m_pSSL, pBuf, iNum);
    }
    
    int Write(const void *pBuf, int iNum)
    {
    	if(!m_psslCtx || !m_pSSL || !m_bConnected)
    	return 0;
    	return SSL_write(m_pSSL, pBuf, iNum);
    }
    Last edited by Gunaamirthavelu; March 24th, 2006 at 06:55 AM.
    If I Helped You, "Rate This Post"

    Thanks
    Guna

  5. #5
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    1,051

    Re: How to convert cpp to c?

    Quote Originally Posted by Gunaamirthavelu
    use this c code
    id like to kno how the code you attached is in C
    - Sreehari
    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
    " Everybody is sent to Earth on a purpose. I am so Lagging behind that i won't die." – Calvin

  6. #6
    Join Date
    Jun 2005
    Location
    Tirunelveli-Tamil Nadu-India
    Posts
    354

    Re: How to convert cpp to c?

    Quote Originally Posted by sreehari
    id like to kno how the code you attached is in C
    what?i can't understand
    If I Helped You, "Rate This Post"

    Thanks
    Guna

  7. #7
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    Re: How to convert cpp to c?

    If you are using sockets then IMHO maybe you should try writing the code using WinSock API, that will be much easier than converting the code to C. But, thats just my opinion.
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

  8. #8
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    Re: How to convert cpp to c?

    If you are using sockets then IMHO you should use WinSock API rather converting your code to C. But, thats just my opinion.
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured