C ++ Converting int to * LONG

I have the following code:

STDMETHODIMP CWrapper::openPort(LONG* m_OpenPortResult)
{
    string str("test");
    const char * c = str.c_str();

    m_OpenPortResult=Open(c); //this does not work because "Open" returns an int

    return S_OK;
}

int Open(const char* uKey)
{

}

I cannot convert "int" to "LONG *". The compiler tells me that "int" cannot be converted to "LONG *".

I also tried using INT * instead of LONG *, but that also gave me an error.

Can someone tell me how can I convert int to LONG * or INT *?

+3
source share
2 answers

You do not need to convert anything. LONG*is a pointer to LONG, and you can assign inta LONG. Just look for a pointer so you can assign it:

*m_OpenPortResult = Open(c); // <-- note the *

Or safer:

if (!m_OpenPortResult) return E_POINTER;
*m_OpenPortResult) = Open(c);

Or even:

LONG ret = Open(c);
if (m_OpenPortResult) *m_OpenPortResult = ret;
+1
source

, , openPort, .

*m_OpenPortResult = Open(c);

, m_OpenPortResult. , . - ++. ( openPort -), ,

STDMETHODIMP CWrapper::openPort(LONG &m_OpenPortResult)
{
    // code...
    m_OpenPortResult = Open(c);
    return S_OK;
}

LONG yourResult;
wrapperInstance.openPort(yourResult); // without & before!

, , .

+4

All Articles