Solved

Use of ReadFile in MFC Activex Control

Posted on 2003-11-21
13
343 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
lucky13 challenge 11 154
Window placement 17 87
Need to impress with my knowledge of .NET 5 93
Windows ICD FFU Issue 4 60
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…

856 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