Solved

Use of ReadFile in MFC Activex Control

Posted on 2003-11-21
13
339 Views
Last Modified: 2013-11-25
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
Comment
Question by:CindyZhou
  • 7
  • 5
13 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9801654
Can u show some part of code (ActiveX) that receives and process the BSTR*?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9801785
long CIOControlCtrl::RecvData(LPBYTE DataRead, long Length, long Timeout, short EncryptLevel)

in VB:

Dim bArray[1000] as Byte


ioctrl.RecvData(bArray, 1000, ...)
0
 

Author Comment

by:CindyZhou
ID: 9810819
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
 

Author Comment

by:CindyZhou
ID: 9811035
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
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9811195
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
 

Author Comment

by:CindyZhou
ID: 9811357
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9811376
Just pass as Reference

Good Luck
0
 

Author Comment

by:CindyZhou
ID: 9811445
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
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9811446
please quote the function declaration of RecvData in VB
0
 

Author Comment

by:CindyZhou
ID: 9811490
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
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 125 total points
ID: 9811584
*DataRead = ::SysAllocString(sDateBuffer);

Rosh :)
0
 

Author Comment

by:CindyZhou
ID: 9811777

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
 

Author Comment

by:CindyZhou
ID: 9813845
roshmon:

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

It works fine. Thanks.

cindy
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now