Connecting Tech Pros Worldwide Forums | Help | Site Map

VC++ 6.0 MFC Application linking to a Win32 DLL issue with pointer to a struct

Newbie
 
Join Date: Oct 2007
Posts: 7
#1: Oct 8 '07
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!

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,167
#2: Oct 8 '07

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
#3: Oct 9 '07

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
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,167
#4: Oct 9 '07

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
#5: Oct 9 '07

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:

Expand|Select|Wrap|Line Numbers
  1. //Header File external.h
  2.  
  3. typedef struct TYPEA
  4. {
  5.     int A
  6.     int B
  7.     char C
  8. }typea;
  9.  
  10. typedef struct TYPEB
  11. {
  12.     BYTE var2
  13.     BYTE var1
  14.     typea temp[10]
  15. }typeb;
  16.  
  17. #ifdef __cplusplus
  18. extern "C"
  19.  ... etc...
  20.  
  21. // Class header file
  22.  
  23. #include "external.h"
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // myMicro window
  27.  
  28. class myMicro : public CWnd
  29. {
  30. // Construction
  31. public:
  32.     myMicro();
  33.  
  34.  
  35. // Attributes
  36. public:
  37.     typeb *tpiData;
  38.  
... 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...
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,167
#6: Oct 9 '07

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
#7: Oct 9 '07

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

Expand|Select|Wrap|Line Numbers
  1. // smw_oem.h
  2. #ifndef _SMW_OEM_H
  3. #define _SMW_OEM_H
  4.  
  5. #if defined (SMW_EXPORT_DLL)
  6.     #define SMWAPI __declspec( dllexport )
  7. #else 
  8.     #define SMWAPI __declspec( dllimport )     
  9. #endif
  10.  
  11. typedef struct  ALARMREC
  12. {
  13.     int Flag;                    //Read/Write flag (1= not yet read; 0= read)
  14.     int AlarmType;                //0= Intrepid Cable; 1= Auxiliary Input 
  15.                                 //11= PM tamper; 12= LU tamper; 13= RM tamper;
  16.     int AlarmUnitNum;             //PM number; Auxiliary number;
  17.     int AlarmUnitWing;            //When AlarmType = 1, 
  18.                                 //1: PM Auxiliary Input
  19.                                 //2: LU Auxiliary Input
  20.                                 //3: RM Auxiliary Input
  21.     int AlarmLocation;            //When Alarm Type = 0,  Subcell Number for Intrepid Cable Alarm;
  22.                                 //When Alarm Type = 1,  2 = Sensor Alarm, 0 = Tamper 1, 1 = Tamper 2
  23.     int AlarmDelayFlag;            //0= Real Time; 1= Delayed;
  24.     char AlarmTime[20];            //Time Stamp for the alarm
  25. }alarmRec;
  26.  
  27. typedef struct SMI_IO
  28. {
  29.     BYTE ErrorCode;
  30.     BYTE DisplaySegmentStatus[50];        //Access/Secure Status for Display Segments 1-50
  31.     BYTE AuxiliarySensorStatus[60][3];    //A/S (Aux.1-Aux.60;Index2 is 0:TP1; 1:TP2; 2:Sensor)
  32.     BYTE PMTamperStatus[8];         //Tamper Access/Secure Status (PM1-PM8 available)
  33.     BYTE LUTamperStatus[9];         //Tamper Access/Secure Status (LU1-LU9 available)
  34.     BYTE RMTamperStatus[20];         //Tamper Access/Secure Status (RM1-RM20 available)
  35.     BYTE DisplaySegmentAlarmState[50];    //In Alarm or Normal (Seg#1-Seg#50 available)
  36.     BYTE AuxiliaryAlarmState[60][3];//Alarm(Aux.1-Aux.60;Index2 is 0:TP1; 1:TP2;2:Sensor)
  37.     BYTE PMTamperAlarmState[8];     //Tamper In Alarm or Normal (PM1-PM8 available)
  38.     BYTE LUTamperAlarmState[9];     //Tamper In Alarm or Normal (LU1-LU9 available)
  39.     BYTE RMTamperAlarmState[20];     //Tamper In Alarm or Normal (RM1-RM20 available)
  40.     BYTE CableAFaultState[8];        //Cable Fault or Normal (PM1-PM8 available)
  41.     BYTE CableBFaultState[8];        //Cable Fault or Normal (PM1-PM8 available)
  42.     BYTE PMComFailState[8];         //Communication Fail or Normal (PM1-PM8 available) 
  43.     BYTE RMComFailState[20];         //Communication Fail or Normal (RM1-RM20 available)
  44.     alarmRec AlarmData[10];
  45. } SmiIo;
  46.  
  47. #ifdef __cplusplus 
  48. extern "C"  
  49. #endif
  50. int gObjID;
  51. #ifdef __cplusplus 
  52. extern "C"  
  53. #endif
  54. void SMWAPI SecureDispSeg(CString portName,int number);
  55. #ifdef __cplusplus 
  56. extern "C"  
  57. #endif
  58. void SMWAPI AccessDispSeg(CString portName,int number);
  59. #ifdef __cplusplus 
  60. extern "C"  
  61. #endif
  62. void SMWAPI SecurePM(CString portName,int number);
  63. #ifdef __cplusplus 
  64. extern "C"  
  65. #endif
  66. void SMWAPI AccessPM(CString portName,int number);
  67. #ifdef __cplusplus 
  68. extern "C"  
  69. #endif
  70. void SMWAPI SecureLU(CString portName,int number);
  71. #ifdef __cplusplus 
  72. extern "C"  
  73. #endif
  74. void SMWAPI AccessLU(CString portName,int number);
  75. #ifdef __cplusplus 
  76. extern "C"  
  77. #endif
  78. void SMWAPI SecureRM(CString portName,int number);
  79. #ifdef __cplusplus 
  80. extern "C"  
  81. #endif
  82. void SMWAPI AccessRM(CString portName,int number);
  83. #ifdef __cplusplus 
  84. extern "C"  
  85. #endif
  86. void SMWAPI SecureAuxiliaryTP1(CString portName,int number);
  87. #ifdef __cplusplus 
  88. extern "C"  
  89. #endif
  90. void SMWAPI AccessAuxiliaryTP1(CString portName,int number);
  91. #ifdef __cplusplus 
  92. extern "C"  
  93. #endif
  94. void SMWAPI SecureAuxiliaryTP2(CString portName,int number);
  95. #ifdef __cplusplus 
  96. extern "C"  
  97. #endif
  98. void SMWAPI AccessAuxiliaryTP2(CString portName,int number);
  99. #ifdef __cplusplus 
  100. extern "C"  
  101. #endif
  102. void SMWAPI SecureAuxiliarySensor(CString portName,int number);
  103. #ifdef __cplusplus 
  104. extern "C"  
  105. #endif
  106. void SMWAPI AccessAuxiliarySensor(CString portName,int number);
  107. #ifdef __cplusplus 
  108. extern "C"  
  109. #endif
  110. void SMWAPI TurnOnRelaysOnOneZone(CString portName,int number);
  111. #ifdef __cplusplus 
  112. extern "C"  
  113. #endif
  114. void SMWAPI TurnOffRelaysOnOneZone(CString portName,int number);
  115. #ifdef __cplusplus 
  116. extern "C"  
  117. #endif
  118. void SMWAPI TurnOnARelayControl(CString portName,int number);
  119. #ifdef __cplusplus 
  120. extern "C"  
  121. #endif
  122. void SMWAPI TurnOffARelayControl(CString portName,int number);
  123. #ifdef __cplusplus 
  124. extern "C"  
  125. #endif
  126. int  SMWAPI SMIAPI_Exit(CString portName);
  127. #ifdef __cplusplus 
  128. extern "C"  
  129. #endif
  130. int SMWAPI SMIAPI_Init(CString portName,LPCSTR mapName,int pcNum,SmiIo **array);
  131. #endif
  132.  
This is the header file to my class
Expand|Select|Wrap|Line Numbers
  1. #if !defined(AFX_SWMICRO_H__DA43CD12_A309_49A9_8921_75B0D5C51FB9__INCLUDED_)
  2. #define AFX_SWMICRO_H__DA43CD12_A309_49A9_8921_75B0D5C51FB9__INCLUDED_
  3.  
  4. #if _MSC_VER > 1000
  5. #pragma once
  6. #endif // _MSC_VER > 1000
  7. // swMicro.h : header file
  8. //
  9.  
  10. #include "smw_oem.h"
  11.  
  12. /////////////////////////////////////////////////////////////////////////////
  13. // swMicro window
  14.  
  15. class swMicro : public CWnd,  public SMI_IO
  16. {
  17. // Construction
  18. public:
  19.     swMicro();
  20.  
  21.  
  22. // Attributes
  23. public:
  24.     SmiIo *tpiData;
  25.  
  26.  
  27. // Operations
  28. public:
  29.     bool Connect();
  30.     bool Disconnect();
  31.  
  32. // Overrides
  33.     // ClassWizard generated virtual function overrides
  34.     //{{AFX_VIRTUAL(swMicro)
  35.     //}}AFX_VIRTUAL
  36.  
  37. // Implementation
  38. public:
  39.     virtual ~swMicro();
  40.  
  41.     // Generated message map functions
  42. protected:
  43.     //{{AFX_MSG(swMicro)
  44.     afx_msg void OnTimer(UINT nIDEvent);
  45.     //}}AFX_MSG
  46.     DECLARE_MESSAGE_MAP()
  47. };
  48.  
  49. /////////////////////////////////////////////////////////////////////////////
  50.  
  51. //{{AFX_INSERT_LOCATION}}
  52. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  53.  
  54. #endif // !defined(AFX_SWMICRO_H__DA43CD12_A309_49A9_8921_75B0D5C51FB9__INCLUDED_)
  55.  
this is the source file to the class

Expand|Select|Wrap|Line Numbers
  1. // swMicro.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "testClass.h"
  6. #include "swMicro.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // swMicro
  16.  
  17. swMicro::swMicro()
  18. {
  19.     //Constructor
  20.     tpiData = NULL;
  21. }
  22.  
  23. swMicro::~swMicro()
  24. {
  25. }
  26.  
  27.  
  28. BEGIN_MESSAGE_MAP(swMicro, CWnd)
  29.     //{{AFX_MSG_MAP(swMicro)
  30.     ON_WM_TIMER()
  31.     //}}AFX_MSG_MAP
  32. END_MESSAGE_MAP()
  33.  
  34.  
  35. /////////////////////////////////////////////////////////////////////////////
  36. // swMicro message handlers
  37.  
  38. void swMicro::OnTimer(UINT nIDEvent) 
  39. {
  40.     // TODO: Add your message handler code here and/or call default
  41.  
  42.     CWnd::OnTimer(nIDEvent);
  43. }
  44.  
  45. /////////////////////////////////////////////////////////////////////////////
  46. // swmicro functions
  47.  
  48. bool swMicro::Connect()
  49. {
  50.     int ret = SMIAPI_Init("COM3","C:\\MapFiles\\SWMW.smp",1,&tpiData);
  51.     return(0);
  52. }
  53.  
  54. bool swMicro::Disconnect()
  55. {
  56.     //TODO:
  57.     return(0);
  58. }
  59.  
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
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,167
#8: Oct 9 '07

re: VC++ 6.0 MFC Application linking to a Win32 DLL issue with pointer to a struct


Expand|Select|Wrap|Line Numbers
  1. 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.
Attached Files
File Type: zip debugingStruct.zip (27.0 KB, 11 views)
Newbie
 
Join Date: Oct 2007
Posts: 7
#9: Oct 9 '07

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*
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,167
#10: Oct 9 '07

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...
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,167
#11: Oct 9 '07

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

Expand|Select|Wrap|Line Numbers
  1.     int ret = SMIAPI_Init("COM3","C:\\MapFiles\\SWMW.smp",1,&tpiData);
to

Expand|Select|Wrap|Line Numbers
  1.     int ret = SMIAPI_Init("COM3","C:\\MapFiles\\SWMW.smp",1,&this->tpiData);
</clutching at straws>
Newbie
 
Join Date: Oct 2007
Posts: 7
#12: Oct 9 '07

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...
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,167
#13: Oct 9 '07

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
#14: Oct 9 '07

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!!!
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,167
#15: Oct 9 '07

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.
Reply