• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

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
0
CindyZhou
Asked:
CindyZhou
  • 7
  • 5
1 Solution
 
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
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
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
 
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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now