?
Solved

Use of ReadFile in MFC Activex Control

Posted on 2003-11-21
13
Medium Priority
?
348 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…

800 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