Solved

C++ dll with Mscomm control: Debug assertion failed error

Posted on 2007-04-08
15
773 Views
Last Modified: 2011-10-03
Hi, I need to create a Visual C++ dll to be called by a VB6 program. In the C++ dll, an MSComm control is required for serial communication. However, when my VB6 program calls the below, it will hit a "Debug Assertion failed" error. Pardon me as I am new to c++.
I know this implementation of using VB6-->C++--> MSComm sounds weird but is necessary to avoid changing tonnes of programs.
Please advise, it's urgent.





// VCDLLA.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "VCDLLA.h"
#include "mscomm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


BEGIN_MESSAGE_MAP(CVCDLLAApp, CWinApp)
      
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVCDLLAApp construction

CVCDLLAApp::CVCDLLAApp()
{
      // TODO: add construction code here,
      // Place all significant initialization in InitInstance

}


/////////////////////////////////////////////////////////////////////////////
// The one and only CVCDLLAApp object

CVCDLLAApp theApp;


_declspec(dllexport) int ReportVersion() {
      AFX_MANAGE_STATE(AfxGetStaticModuleState());
      CMSComm m_comm;
      m_comm.SetCommPort(1);
      m_comm.GetCommPort();
      m_comm.SetCommPort(1);
      m_comm.SetSettings("9600,o,8,1");
      m_comm.SetInputLen(1);
      m_comm.SetInputMode(0);
      m_comm.SetRTSEnable(TRUE);
      m_comm.SetRThreshold(1);
      m_comm.SetSThreshold (1);
      m_comm.SetPortOpen(true);
      m_comm.SetOutput(COleVariant("Hello456"));
    return 13;
}
0
Comment
Question by:shiaoyan05
  • 6
  • 5
  • 4
15 Comments
 
LVL 10

Expert Comment

by:sakuya_su
ID: 18874170
Debug assertion failed error

that is because in your C++ code, there is an Assert command somewhere checking an condition, you will need to look through the code and find where this assert is and see why it is being triggered
0
 

Author Comment

by:shiaoyan05
ID: 18874241
I have looked through the code but did not find any assert command.
Please advise.
I was just wondering if my instantiation of the MSComm object should work this way?
0
 
LVL 10

Expert Comment

by:sakuya_su
ID: 18874320
   m_comm.SetCommPort(1);
      m_comm.GetCommPort();
      m_comm.SetCommPort(1);

why is it like that?

Other than that i see your code is correct in creating a new comm....
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:shiaoyan05
ID: 18874438
Hi,
It is supposed to be
CMSComm m_comm;
m_comm.SetCommPort(1);
m_comm.SetSettings("9600,o,8,1");

My VB program will then call c++ ReportVersion() method. This is when I will hit the assertion error, at the first line: m_comm.SetCommPort(1);

Thanks
0
 
LVL 10

Expert Comment

by:sakuya_su
ID: 18874707
Had a look:

void AFX_CDECL CWnd::InvokeHelper(DISPID dwDispID, WORD wFlags, VARTYPE vtRet,
      void* pvRet, const BYTE* pbParamInfo, ...)
{
      ASSERT(m_pCtrlSite != NULL);

That is called on the m_comm.SetCommPort(1);

this indicates that you do not have MSComm component registered..

you should either install it from the VC++ CD..... or.. use regsvr32 on it
0
 
LVL 10

Expert Comment

by:sakuya_su
ID: 18874713
to be more specific... you need to regsvr32 mscomm32.ocx

then you need to add reference to it in your C++ project.. which I, unfortunately have no idea how to do as I have not done alot of SDI stuff

maybe someone else here can say more on this.
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 500 total points
ID: 18875002
Do you have problem to compile your dll in release mode and call from Vb?
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 18875012
Also check on this link

http://www.morovia.com/supportforum/forum_posts.asp?TID=53&PN=1
>>Here's a quick example of how to use the control. It's important to wait until your OnInitDialog() call (or anytime after) before trying to use it because not everything will be created/initialized before this
0
 

Author Comment

by:shiaoyan05
ID: 18875154
I manage to run without error when compiled in a Release version. However, nothing is displayed. It is a working code. I also cannot create a dialog as I need it to be built as a dll. Pardon my questions as I am a newbie
0
 

Author Comment

by:shiaoyan05
ID: 18875190
When calling from VB6 i managed to retrieve the commport throught the C++ method:
However, the comm port returned is 19 though I have set it to 1.
Please help

C++ code:
===
 CMSComm m_comm;
      m_comm.SetCommPort(1);
      m_comm.SetSettings("9600,o,8,1");
      m_comm.SetInputLen(1);
      m_comm.SetInputMode(0);
      m_comm.SetRTSEnable(TRUE);
      m_comm.SetRThreshold(1);
      m_comm.SetSThreshold (1);
      m_comm.SetPortOpen(true);
      m_comm.SetOutput(COleVariant("Hello456"));
return m_comm.GetCommPort();
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 18876294
You may want to move this line to your header file and try. Give it sometime to initialize

CMSComm m_comm;
0
 

Author Comment

by:shiaoyan05
ID: 18879868
Hi,
If I put it at different parts of the codes/ header, it will return a different port number eg. 836 etc.
This code will run properly in exe but hit issues when running dll. can anyone help to provide a sample for using mscomm in dll?
0
 
LVL 10

Expert Comment

by:sakuya_su
ID: 18880474
hmm.... does this dll generate any forms? buttons etc?
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 18881291
is it possible your com1 already open by something?

what is m_comm.GetPortOpen()
0
 

Author Comment

by:shiaoyan05
ID: 18906392
Hi Thanks to all who have helped so far. I did not manage to get it to work though it compiled correctly..
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

776 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