Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hello Everyone,

Am working creating MFC DLL & am getting an error:Unhandled exception at 0x00000000 in Key_Testing.exe: 0xC0000005: Access violation.

The DLL Code is like this:
//Demo.h
char*	GetDiskSerialNumber (char g_DiskLetter);
 
//Demo.cpp
char* DLLclass::GetDiskSerialNumber(char g_DiskLetter)
{
	CHAR SN[64];
	memset(SN,0,sizeof(SN));
		
	if(GetSerialNumber(SN,g_DiskLetter)){   // Error in this Line..
		return (char*)SN;
	}
}
 
This DLL am using in an application..
void CKey_TestingDlg::OnBnClickedgetserial()
{
	// TODO: Add your control notification handler code here
	DLLclass Dllobj;
	char ltr='h';
	char* ser=Dllobj.GetDiskSerialNumber(ltr);
}

Please,, Could anyone suggest where i gone wrong.. Any help will be appriciated..

Thank you.
Posted 24-Jan-12 19:52pm
Guru_C++1.2K
Edited 24-Jan-12 19:55pm
v2
Comments
Lakamraju Raghuram at 25-Jan-12 1:20am
   
Can you give info about GetSerialNumber(...) method you are using
Gurubasavaraj at 25-Jan-12 2:05am
   
Thanks for your help :) Function Declaration for GetSerialNumber is like this:
typedef BOOL (WINAPI *GetSerialNumberDLL)(CHAR* , CHAR );
johny10151981 at 25-Jan-12 2:31am
   
boolian pointer? are you sure?
Gurubasavaraj at 25-Jan-12 2:17am
   
I changed my code as per as ur suggestion.. Still a getting same error..:(
Gurubasavaraj at 25-Jan-12 3:08am
   
Yes John.. Its boolian pointer..
Stefan_Lang at 25-Jan-12 4:17am
   
Erm, no...

If my understanding of function pointers serves me well, the type you defined is a pointer to a function that returns BOOL, not a function that returns pointer to BOOL as John implied.

But I might be wrong, I never use function pointers myself ;)

P.S.: If my understanding is correct, then shouldn't you invoke the function like this:
if ((*GetSerialNumber)(...))
? (just guessing, I might be wrong)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

What is the code inside GetSerialNumber?
There is probably some error there.

Also, you're declaring a local array and returning its address, which will no longer be valid when the function returns.

Instead change it as follows -
void DLLclass::GetDiskSerialNumber(char g_DiskLetter, char* SN)
{
	GetSerialNumber(SN, g_DiskLetter);
}
 
void CKey_TestingDlg::OnBnClickedgetserial()
{
	// TODO: Add your control notification handler code here
	DLLclass Dllobj;
	char ltr='h';
 
	CHAR SN[64];
	memset(SN,0,sizeof(SN));
 
	Dllobj.GetDiskSerialNumber(ltr, SN);
}
  Permalink  
Comments
SAKryukov at 25-Jan-12 1:25am
   
Aha, good catch. My 5.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

GetDiskSerialNumber is returning a pointer to a local array. This is not valid. Local variables die when the function exits as they reside on the stack.
  Permalink  
Comments
Gurubasavaraj at 25-Jan-12 7:54am
   
Yes ur correct.. And, Also am derefrencing a null pointer. But, am not getting how to modify the code..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Sergey Alexandrovich Kryukov 133
2 Sebastiaan Meijerink 105
3 Rana Waqas 100
4 Abhinav S 95
0 Sergey Alexandrovich Kryukov 6,558
1 OriginalGriff 6,268
2 Peter Leow 2,534
3 Abhinav S 2,344
4 Maciej Los 2,297


Advertise | Privacy | Mobile
Web04 | 2.8.150414.1 | Last Updated 25 Jan 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100