Use of ReadFile in MFC Activex Control

Hi,

I am building an MFC Activex Control to receive and send data from serial port. My Receive function is as follows:

ong CIOControlCtrl::RecvData(LPUNKNOWN DataRead, long Length, long Timeout, short EncryptLevel)
{
      DWORD readLen;
   //m_hportIn is a handle to a successfully opened port
    if(!ReadFile(m_hportIn, DataRead, Length, &readLen, NULL)){
            AfxMessageBox("\nerror reading serial port.\n");
            return 0;
    }

      return readLen;
}

I can build the control successfully. But when I tested the method in visual basic, I passed a String type argument for DataRead, it always gives type mismatch.

So in the control itself, I changed the datatype of the DataRead in the RecvData to BSTR*. Now when test in VB, it did execute though it gave fatal error that i have to close the whole program.

Anyone has any idea what kind data type should I use for my RecvData method?

In addition, I found we can only choose the data type from the list in the mfc Activex  control automation method. If I manually change a data type which is not listed, I will have trouble to open class wizard later on, however, the control seems OK. Why is it like this? Is it good to do it this way?

Thanks,

Cindy
CindyZhouAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roshan DavisCommented:
Can u show some part of code (ActiveX) that receives and process the BSTR*?
0
AlexFMCommented:
long CIOControlCtrl::RecvData(LPBYTE DataRead, long Length, long Timeout, short EncryptLevel)

in VB:

Dim bArray[1000] as Byte


ioctrl.RecvData(bArray, 1000, ...)
0
CindyZhouAuthor Commented:
AlexFM:


Thanks for the suggestion. But the problem is that when add method to the contorl, there is no "LPBYTE" data type for argument. I can manually force it to LPBYTE, but later on I cannot even open the classwizard in c++. Any idea?

Thanks,
Cindy




0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

CindyZhouAuthor Commented:
roshmon:

Thanks for the reply. The RecvData is very simple, basically it only contains ReadFile function.

In ActiveX control, the code is following:

long CIOControlCtrl::RecvData(BSTR* DataRead, long Length, long Timeout, short EncryptLevel)
{
      DWORD readLen;
      char buffer[10];

    if(!ReadFile(m_hportIn, DataRead, Length, &readLen, NULL)){
            AfxMessageBox("\nerror reading serial port.\n");
            return 0;
    }

      return readLen;
}


In VB:

   Dim readData As String
   Dim readLen As Long
   
   length = 5
   readLen = IOControl1.RecvData(readData, 5, 60, 0)
   MsgBox readLen

Cindy
0
Roshan DavisCommented:
Try this

long CIOControlCtrl::RecvData(BSTR* DataRead, long Length, long Timeout, short EncryptLevel)
{
    DWORD readLen;
    char buffer[10];
    TCHAR sDateBuffer[250];

   if(!ReadFile(m_hportIn, sDateBuffer, Length, &readLen, NULL)){
          AfxMessageBox("\nerror reading serial port.\n");
          return 0;
   }

    *DataRead = _bstr_t(sDateBuffer).copy();

    return readLen;
}


Good Luck
0
CindyZhouAuthor Commented:
roshmon:

Thanks for the advice.  I just tried it. When I tested in VB, I pass a string type for DataRead, it gives a compile error "type mismatch".

Any idea?

Cindy
0
Roshan DavisCommented:
Just pass as Reference

Good Luck
0
CindyZhouAuthor Commented:
roshmon:

  How to pass as reference when calling ActiveX control method?


Below is my VB code using C++ control:
=============================================
   Dim readData As String
   Dim length As Long
   Dim readLen As Long
   
   length = 5
   readLen = IOControl1.RecvData(readData, length, 60, 0)
   MsgBox readLen
=============================================

Cindy

0
Roshan DavisCommented:
please quote the function declaration of RecvData in VB
0
CindyZhouAuthor Commented:
roshmon:

RecvData is the method of my c++ ActiveX control. So in VB, I can just use this method, there is nowhere to declare it in vb.

Cindy
0
Roshan DavisCommented:
*DataRead = ::SysAllocString(sDateBuffer);

Rosh :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CindyZhouAuthor Commented:

DataRead = ::SysAllocString(sDateBuffer);

What is the data type of sDateBuffer? I tried TCHAR, it did not work. I tried OLECHAR*, but I don't know how to initialize the sDateBuffer.
0
CindyZhouAuthor Commented:
roshmon:

*DataRead = ::SysAllocString((OLECHAR*)Buffer);

It works fine. Thanks.

cindy
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.