VC++ 6.0 MFC Application linking to a Win32 DLL issue with pointer to a struct | Newbie | | Join Date: Oct 2007
Posts: 7
| | |
I'm hoping someone has seen this before ...
I have a header file that looks something like this
typedef struct ATYPE
{
int one;
int two;
char first;
}atype;
typedef struct BTYPE
{
BYTE FIRST;
BYTE SECOND;
atype aData[10];
}btype;
the some functions for the DLL I am linking too...
in my code I add a member varable like
public:
btype *array;
when I stop execution just after that statement I see the
Name Value
&array 0x0012f9e4 "III xA"
but I dont see
array 0x001245ef {...}
|- FIRST 0
SECOND 0
the demo app that I have runs correctly however when I try to recreate the code I get an access violation when trying to pass &array to a function...
it seems I am missing some kind of directive or something but thus far have not been able to track it down...
PLEASE if anyone has seen anything like this before let me know ...
Thank you!
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,167
| | | re: VC++ 6.0 MFC Application linking to a Win32 DLL issue with pointer to a struct
It sounds like you are using pointers without initialising them by allocating memory from the heap for the data they point to.
| | Newbie | | Join Date: Oct 2007
Posts: 7
| | | re: VC++ 6.0 MFC Application linking to a Win32 DLL issue with pointer to a struct Quote:
Originally Posted by Banfa It sounds like you are using pointers without initialising them by allocating memory from the heap for the data they point to. Thats exactly what it sounds like, however even when I change the statement to read
typeb array = new typeb; I still get the same access violation error in MFC42.DLL
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,167
| | | re: VC++ 6.0 MFC Application linking to a Win32 DLL issue with pointer to a struct
Run it in the debugger and when it crashes look at the call stack and trace up the call stack to see if any of the functions are being called with invalid parameters. Then determin why the parameters are not valid.
| | Newbie | | Join Date: Oct 2007
Posts: 7
| | | re: VC++ 6.0 MFC Application linking to a Win32 DLL issue with pointer to a struct
Thank You! i'm getting closer to understanding whats going on here..
I have a header file with the typedef struct statements and the extern statements that point to functions in a win32dll
when I create a MFC Win dialog app with VC++ 6.0 and place my header file as an include in the Dlg.h file and create a pointer to one of my struct types it works correctly ...
however ... when I create a class and include that same header file ...
and create a pointer to the same struct it becomes type char* instead of my typeb*
ex: -
//Header File external.h
-
-
typedef struct TYPEA
-
{
-
int A
-
int B
-
char C
-
}typea;
-
-
typedef struct TYPEB
-
{
-
BYTE var2
-
BYTE var1
-
typea temp[10]
-
}typeb;
-
-
#ifdef __cplusplus
-
extern "C"
-
... etc...
-
-
// Class header file
-
-
#include "external.h"
-
-
/////////////////////////////////////////////////////////////////////////////
-
// myMicro window
-
-
class myMicro : public CWnd
-
{
-
// Construction
-
public:
-
myMicro();
-
-
-
// Attributes
-
public:
-
typeb *tpiData;
-
... this is the problem ...
*tpiData becomes char* and not a typeb* ...
but only if I create a class from it ... it seesm like the typeb is out of scope but im not getting any undefined type errors during compile ...I just get a access violation when I try to pass the char* to a function that expects the typeb*...
im at a loss here.. i need this to be an external class .. but it seems the only way i can get it work work is in a Dlg class...
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,167
| | | re: VC++ 6.0 MFC Application linking to a Win32 DLL issue with pointer to a struct Quote:
Originally Posted by actcom I have a header file with the typedef struct statements and the extern statements that point to functions in a win32dll If possible you should not do this, you should just include the header file the function are defined in. Quote:
Originally Posted by actcom *tpiData becomes char* and not a typeb* ... I sorry but this really doesn't make sense to me (BTW did you mean tpiData rather than *tpiData), you declare tpiData as typeb *, then that is it's type. It does not become another type unless your code chooses to treat it that way (by casting it for example).
Can you post the code you use to allocate data for this pointer, the section of your code where the crash is happening?
BTW when posting code please use code tags, read "How to ask a question" from our forum FAQ.
| | Newbie | | Join Date: Oct 2007
Posts: 7
| | | re: VC++ 6.0 MFC Application linking to a Win32 DLL issue with pointer to a struct Quote:
Originally Posted by Banfa If possible you should not do this, you should just include the header file the function are defined in.
I sorry but this really doesn't make sense to me (BTW did you mean tpiData rather than *tpiData), you declare tpiData as typeb *, then that is it's type. It does not become another type unless your code chooses to treat it that way (by casting it for example).
Can you post the code you use to allocate data for this pointer, the section of your code where the crash is happening?
BTW when posting code please use code tags, read "How to ask a question" from our forum FAQ.
I meant *tpiData yes...
here is the exact code of the header file that defines the external Win32dll -
// smw_oem.h
-
#ifndef _SMW_OEM_H
-
#define _SMW_OEM_H
-
-
#if defined (SMW_EXPORT_DLL)
-
#define SMWAPI __declspec( dllexport )
-
#else
-
#define SMWAPI __declspec( dllimport )
-
#endif
-
-
typedef struct ALARMREC
-
{
-
int Flag; //Read/Write flag (1= not yet read; 0= read)
-
int AlarmType; //0= Intrepid Cable; 1= Auxiliary Input
-
//11= PM tamper; 12= LU tamper; 13= RM tamper;
-
int AlarmUnitNum; //PM number; Auxiliary number;
-
int AlarmUnitWing; //When AlarmType = 1,
-
//1: PM Auxiliary Input
-
//2: LU Auxiliary Input
-
//3: RM Auxiliary Input
-
int AlarmLocation; //When Alarm Type = 0, Subcell Number for Intrepid Cable Alarm;
-
//When Alarm Type = 1, 2 = Sensor Alarm, 0 = Tamper 1, 1 = Tamper 2
-
int AlarmDelayFlag; //0= Real Time; 1= Delayed;
-
char AlarmTime[20]; //Time Stamp for the alarm
-
}alarmRec;
-
-
typedef struct SMI_IO
-
{
-
BYTE ErrorCode;
-
BYTE DisplaySegmentStatus[50]; //Access/Secure Status for Display Segments 1-50
-
BYTE AuxiliarySensorStatus[60][3]; //A/S (Aux.1-Aux.60;Index2 is 0:TP1; 1:TP2; 2:Sensor)
-
BYTE PMTamperStatus[8]; //Tamper Access/Secure Status (PM1-PM8 available)
-
BYTE LUTamperStatus[9]; //Tamper Access/Secure Status (LU1-LU9 available)
-
BYTE RMTamperStatus[20]; //Tamper Access/Secure Status (RM1-RM20 available)
-
BYTE DisplaySegmentAlarmState[50]; //In Alarm or Normal (Seg#1-Seg#50 available)
-
BYTE AuxiliaryAlarmState[60][3];//Alarm(Aux.1-Aux.60;Index2 is 0:TP1; 1:TP2;2:Sensor)
-
BYTE PMTamperAlarmState[8]; //Tamper In Alarm or Normal (PM1-PM8 available)
-
BYTE LUTamperAlarmState[9]; //Tamper In Alarm or Normal (LU1-LU9 available)
-
BYTE RMTamperAlarmState[20]; //Tamper In Alarm or Normal (RM1-RM20 available)
-
BYTE CableAFaultState[8]; //Cable Fault or Normal (PM1-PM8 available)
-
BYTE CableBFaultState[8]; //Cable Fault or Normal (PM1-PM8 available)
-
BYTE PMComFailState[8]; //Communication Fail or Normal (PM1-PM8 available)
-
BYTE RMComFailState[20]; //Communication Fail or Normal (RM1-RM20 available)
-
alarmRec AlarmData[10];
-
} SmiIo;
-
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
int gObjID;
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI SecureDispSeg(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI AccessDispSeg(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI SecurePM(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI AccessPM(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI SecureLU(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI AccessLU(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI SecureRM(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI AccessRM(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI SecureAuxiliaryTP1(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI AccessAuxiliaryTP1(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI SecureAuxiliaryTP2(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI AccessAuxiliaryTP2(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI SecureAuxiliarySensor(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI AccessAuxiliarySensor(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI TurnOnRelaysOnOneZone(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI TurnOffRelaysOnOneZone(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI TurnOnARelayControl(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
void SMWAPI TurnOffARelayControl(CString portName,int number);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
int SMWAPI SMIAPI_Exit(CString portName);
-
#ifdef __cplusplus
-
extern "C"
-
#endif
-
int SMWAPI SMIAPI_Init(CString portName,LPCSTR mapName,int pcNum,SmiIo **array);
-
#endif
-
This is the header file to my class -
#if !defined(AFX_SWMICRO_H__DA43CD12_A309_49A9_8921_75B0D5C51FB9__INCLUDED_)
-
#define AFX_SWMICRO_H__DA43CD12_A309_49A9_8921_75B0D5C51FB9__INCLUDED_
-
-
#if _MSC_VER > 1000
-
#pragma once
-
#endif // _MSC_VER > 1000
-
// swMicro.h : header file
-
//
-
-
#include "smw_oem.h"
-
-
/////////////////////////////////////////////////////////////////////////////
-
// swMicro window
-
-
class swMicro : public CWnd, public SMI_IO
-
{
-
// Construction
-
public:
-
swMicro();
-
-
-
// Attributes
-
public:
-
SmiIo *tpiData;
-
-
-
// Operations
-
public:
-
bool Connect();
-
bool Disconnect();
-
-
// Overrides
-
// ClassWizard generated virtual function overrides
-
//{{AFX_VIRTUAL(swMicro)
-
//}}AFX_VIRTUAL
-
-
// Implementation
-
public:
-
virtual ~swMicro();
-
-
// Generated message map functions
-
protected:
-
//{{AFX_MSG(swMicro)
-
afx_msg void OnTimer(UINT nIDEvent);
-
//}}AFX_MSG
-
DECLARE_MESSAGE_MAP()
-
};
-
-
/////////////////////////////////////////////////////////////////////////////
-
-
//{{AFX_INSERT_LOCATION}}
-
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
-
-
#endif // !defined(AFX_SWMICRO_H__DA43CD12_A309_49A9_8921_75B0D5C51FB9__INCLUDED_)
-
this is the source file to the class -
// swMicro.cpp : implementation file
-
//
-
-
#include "stdafx.h"
-
#include "testClass.h"
-
#include "swMicro.h"
-
-
#ifdef _DEBUG
-
#define new DEBUG_NEW
-
#undef THIS_FILE
-
static char THIS_FILE[] = __FILE__;
-
#endif
-
-
/////////////////////////////////////////////////////////////////////////////
-
// swMicro
-
-
swMicro::swMicro()
-
{
-
//Constructor
-
tpiData = NULL;
-
}
-
-
swMicro::~swMicro()
-
{
-
}
-
-
-
BEGIN_MESSAGE_MAP(swMicro, CWnd)
-
//{{AFX_MSG_MAP(swMicro)
-
ON_WM_TIMER()
-
//}}AFX_MSG_MAP
-
END_MESSAGE_MAP()
-
-
-
/////////////////////////////////////////////////////////////////////////////
-
// swMicro message handlers
-
-
void swMicro::OnTimer(UINT nIDEvent)
-
{
-
// TODO: Add your message handler code here and/or call default
-
-
CWnd::OnTimer(nIDEvent);
-
}
-
-
/////////////////////////////////////////////////////////////////////////////
-
// swmicro functions
-
-
bool swMicro::Connect()
-
{
-
int ret = SMIAPI_Init("COM3","C:\\MapFiles\\SWMW.smp",1,&tpiData);
-
return(0);
-
}
-
-
bool swMicro::Disconnect()
-
{
-
//TODO:
-
return(0);
-
}
-
when SmiIo *tpiData is decalred it should be type SmiIo however when I use the debugger and break at the method swMicro::Connect &tpiData holds the address of a char* .. that is waht is causing the error when I call SMIAPI_Init
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,167
| | | re: VC++ 6.0 MFC Application linking to a Win32 DLL issue with pointer to a struct - class swMicro : public CWnd, public SMI_IO
Do you really want to subclass from SMI_IO???
Any back to the problem, Quote:
when SmiIo *tpiData is decalred it should be type SmiIo however when I use the debugger and break at the method swMicro::Connect &tpiData holds the address of a char* .. that is waht is causing the error when I call SMIAPI_Init
What makes you think it is the address of a char *? MSVC 6.0 does not give the types of variables watched.
In a similar situation I see &tpiData as an address, this is correct because it is a pointer, when I expand the expression I see another address, this is ok as well because by expanding a dereference the expression so display *&tpiData or tpiData which is a pointer. When I expand that expression I see the items in the SmiIo structure, all invalid because as yet no data has been allocated for them. See attached image.
| | Newbie | | Join Date: Oct 2007
Posts: 7
| | | re: VC++ 6.0 MFC Application linking to a Win32 DLL issue with pointer to a struct
the Subclass was just me trying to work around the issue
tpiData needs to be type SimIo or I cannot pass it to the function ... the compiler obviously sees it as the correct type because it compiles ... and if I try a different type .. like say.. .int .. it will not compile ...
I'm not sure how to add a screenshot here.. but suffice it to say.. when i break at the call to the function .. right click on the &tpiData variable and choose Properties .. the type is char*
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,167
| | | re: VC++ 6.0 MFC Application linking to a Win32 DLL issue with pointer to a struct Quote:
Originally Posted by actcom I'm not sure how to add a screenshot here.. It isn't easy :D Quote:
Originally Posted by actcom but suffice it to say.. when i break at the call to the function .. right click on the &tpiData variable and choose Properties .. the type is char* Very very strange just tried this and I get SMI_IO **,
thinking...
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,167
| | | re: VC++ 6.0 MFC Application linking to a Win32 DLL issue with pointer to a struct
I assume you have no global variables called tpiData?
<clutching at straws>
Try changing - int ret = SMIAPI_Init("COM3","C:\\MapFiles\\SWMW.smp",1,&tpiData);
to - int ret = SMIAPI_Init("COM3","C:\\MapFiles\\SWMW.smp",1,&this->tpiData);
</clutching at straws>
| | Newbie | | Join Date: Oct 2007
Posts: 7
| | | re: VC++ 6.0 MFC Application linking to a Win32 DLL issue with pointer to a struct
dosent make any difference ...
something i've noticed is that &tpiData is always type char* but when I break at the function call ... I see two variables in the window
i see tpiData .. as type SmiIo with all the members of the struct and I see &tpiData as type char* ...reguardless .. when I let the app make the call I get an Access Violation in MFC42.DLL...
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,167
| | | re: VC++ 6.0 MFC Application linking to a Win32 DLL issue with pointer to a struct
Where is SMIAPI_Init defined because I can find no reference to it anywhere.
| | Newbie | | Join Date: Oct 2007
Posts: 7
| | | re: VC++ 6.0 MFC Application linking to a Win32 DLL issue with pointer to a struct
I think I got it ...
For some odd reason .. if I put the .lib file in the debug/release folder instead of the root project folder .. the call works correctly...
Thank you for all your help!!!
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,167
| | | re: VC++ 6.0 MFC Application linking to a Win32 DLL issue with pointer to a struct
No problem, that probably means you have an old .lib floating around on your system somewhere that was getting picked up.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|