Solved

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

Posted on 2007-04-08
15
764 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

760 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now