473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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!
Oct 8 '07 #1
14 3157
Banfa
9,065 Expert Mod 8TB
It sounds like you are using pointers without initialising them by allocating memory from the heap for the data they point to.
Oct 8 '07 #2
actcom
7
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
Oct 9 '07 #3
Banfa
9,065 Expert Mod 8TB
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.
Oct 9 '07 #4
actcom
7
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...
Oct 9 '07 #5
Banfa
9,065 Expert Mod 8TB
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.

*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.
Oct 9 '07 #6
actcom
7
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
Oct 9 '07 #7
Banfa
9,065 Expert Mod 8TB
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,
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, 102 views)
Oct 9 '07 #8
actcom
7
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*
Oct 9 '07 #9
Banfa
9,065 Expert Mod 8TB
I'm not sure how to add a screenshot here..
It isn't easy :D
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...
Oct 9 '07 #10
Banfa
9,065 Expert Mod 8TB
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>
Oct 9 '07 #11
actcom
7
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...
Oct 9 '07 #12
Banfa
9,065 Expert Mod 8TB
Where is SMIAPI_Init defined because I can find no reference to it anywhere.
Oct 9 '07 #13
actcom
7
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!!!
Oct 9 '07 #14
Banfa
9,065 Expert Mod 8TB
No problem, that probably means you have an old .lib floating around on your system somewhere that was getting picked up.
Oct 9 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: yanwan | last post by:
Hello I met some problems in linking a project, and hope someone can give me some advice. -----------Configuration: lighting - Win32 Release-------------------- Linking... LINK : warning...
11
by: Tatu Portin | last post by:
Have this kind of struct: typedef struct { char **user_comments; /* ... */ } vorbis_comment; /* prototype */ char * read_vorbis_string ( FILE *sc);
2
by: Vinayak Raghuvamshi | last post by:
I have a simple, non-managed structure. When I create an array of this struct, only the first item in the array gets created, the others are not. Here is my sample code. This works when I build...
5
by: prashanth | last post by:
Hi All, I have few link errors that i am not able to get rif off in a VC++ project (plugin for filemaker) where i am trying to port the code written for a mac to windows. I am creating a...
37
by: Greg | last post by:
Except for legacy or non-.NET applications, is there any reason to use VC++ anymore? It seems that for .NET applications, there would be no reason to choose C++ over C# since C# is faster to...
4
by: danip | last post by:
Hi, Is it possible to add a VC++ 6 library ot a VC++ 8 application ? if yes how ? is it possible to link a DLL ? how ? -- Dani
0
by: Philip Lowman | last post by:
I am in the process of trying to migrate a couple of build solutions to Visual Studio Express 2005 from VS 2003 Professional and I am running into a weird C/C++ runtime library linking issue when...
10
by: dorkrawk | last post by:
I am having an issue with some VC++ I am writing. I have a struct and I'm trying to call a function from it from a function in another object. here is the struct.... struct JNI_Interface {...
1
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.