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

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

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
shiaoyan05
Asked:
shiaoyan05
  • 6
  • 5
  • 4
1 Solution
 
sakuya_suCommented:
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
 
shiaoyan05Author Commented:
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
 
sakuya_suCommented:
   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
Industry Leaders: 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!

 
shiaoyan05Author Commented:
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
 
sakuya_suCommented:
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
 
sakuya_suCommented:
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
 
EDDYKTCommented:
Do you have problem to compile your dll in release mode and call from Vb?
0
 
EDDYKTCommented:
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
 
shiaoyan05Author Commented:
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
 
shiaoyan05Author Commented:
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
 
EDDYKTCommented:
You may want to move this line to your header file and try. Give it sometime to initialize

CMSComm m_comm;
0
 
shiaoyan05Author Commented:
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
 
sakuya_suCommented:
hmm.... does this dll generate any forms? buttons etc?
0
 
EDDYKTCommented:
is it possible your com1 already open by something?

what is m_comm.GetPortOpen()
0
 
shiaoyan05Author Commented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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