Solved

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

Posted on 2007-04-08
15
791 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
[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
  • 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
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!

 

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

Independent Software Vendors: 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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

729 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