Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. global buffer or passing by reference?

global buffer or passing by reference?

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++question
22 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B billiam904

    First, I am new to C++. I have some C background with DOS and Unix but that was 15 years ago. My most recent work was vb.net. I am now doing some audio based coding in VC++ and I am having some problems, probably from my lack of knowledge. Unfortunately, I have until Monday to get this code working... I have three classes and three separate files. The developer that started this is simply calling the classes without reinstantiating them. I am reading a wave file in and that works fine. But, I need to create a global buffer for the wave data so that the other two classes can access it or find someway to pass the buffer by references to the other classes' functions. Can anyone give me some same code on doing this? Thank you. Bill Dennis Orlando, FL

    K Offline
    K Offline
    khan
    wrote on last edited by
    #2

    Create a class for the global data buffer. Put it in a separate header file. class CGlobalData { static [DATA_TYPE_HERE] m_Data; }; Then use it as: CGlobalData::m_Data; this is this.

    B 1 Reply Last reply
    0
    • K khan

      Create a class for the global data buffer. Put it in a separate header file. class CGlobalData { static [DATA_TYPE_HERE] m_Data; }; Then use it as: CGlobalData::m_Data; this is this.

      B Offline
      B Offline
      billiam904
      wrote on last edited by
      #3

      I did as you suggested and am etting the following error: c:\development\c-tools\Classes\WavWrite.cpp(79): error C2248: 'CReadDemoWave::testinteger' : cannot access private member declared in class 'CReadDemoWave' I need to be able to access this data from three separate .cpp files. Here's my 'h: \ class CReadDemoWave { static int testinteger; public: void ReadDemoWave(); void ReadDemoIntoBuffer(); void PlayWav(); long QueueWaveData(HWAVEOUT HWaveOut, WAVEHDR * waveHeader, DWORD WaveDataPlayed, DWORD WaveBufSize, HMMIO HMmio); LPSTR *GetDemoBuffer(void); void WriteDemo(HMMIO recordHandle); WAVEHDR lpDemoData; DWORD dwDemoDataSize; int m_nDemoBufferSize; BYTE* m_pbyDemoBuffer; HMMIO m_hmmio; }; I am including the .h in all three .cpp files. Am I doing something wrong? Bill Dennis Orlando, FL

      K 1 Reply Last reply
      0
      • B billiam904

        I did as you suggested and am etting the following error: c:\development\c-tools\Classes\WavWrite.cpp(79): error C2248: 'CReadDemoWave::testinteger' : cannot access private member declared in class 'CReadDemoWave' I need to be able to access this data from three separate .cpp files. Here's my 'h: \ class CReadDemoWave { static int testinteger; public: void ReadDemoWave(); void ReadDemoIntoBuffer(); void PlayWav(); long QueueWaveData(HWAVEOUT HWaveOut, WAVEHDR * waveHeader, DWORD WaveDataPlayed, DWORD WaveBufSize, HMMIO HMmio); LPSTR *GetDemoBuffer(void); void WriteDemo(HMMIO recordHandle); WAVEHDR lpDemoData; DWORD dwDemoDataSize; int m_nDemoBufferSize; BYTE* m_pbyDemoBuffer; HMMIO m_hmmio; }; I am including the .h in all three .cpp files. Am I doing something wrong? Bill Dennis Orlando, FL

        K Offline
        K Offline
        khan
        wrote on last edited by
        #4

        Sorry about that. You need to make the variable public. By default it is private. Just write public: before the variable declaration in the class. Like: class CSomething { public: //this needs to be inserted. static DATA_TYPE m_Data; }; this is this.

        B 1 Reply Last reply
        0
        • B billiam904

          First, I am new to C++. I have some C background with DOS and Unix but that was 15 years ago. My most recent work was vb.net. I am now doing some audio based coding in VC++ and I am having some problems, probably from my lack of knowledge. Unfortunately, I have until Monday to get this code working... I have three classes and three separate files. The developer that started this is simply calling the classes without reinstantiating them. I am reading a wave file in and that works fine. But, I need to create a global buffer for the wave data so that the other two classes can access it or find someway to pass the buffer by references to the other classes' functions. Can anyone give me some same code on doing this? Thank you. Bill Dennis Orlando, FL

          A Offline
          A Offline
          Achim Klein
          wrote on last edited by
          #5

          Hi, in my opinion it is always worth an attempt to get around global data. Maybe this design pattern (Visitor) can help you. The class that can read wave files:

          class IveGotTheData
          {
          public:

          // add some 'Getter' methods here

          bool ReadWaveFile(const char* Path);

          void UseMyData(IknowTheAlgorithm& Operator)
          {
          Operator.Operate(this);
          }
          };

          The base class for all algorithms that modify your wave data (the Visitor):

          class IknowTheAlgorithm
          {
          public:

          virtual void Operate(IveGotTheData* Data) = 0;
          };

          Your first algorithm is encapsulated in this class:

          class Algorithm1 : public IknowTheAlgorithm
          {
          public:

          virtual void Operate(IveGotTheData* Data)
          {
          // your first algorithm goes here
          }
          };

          Your second algorithm is encapsulated in this class:

          class Algorithm2 : public IknowTheAlgorithm
          {
          public:

          virtual void Operate(IveGotTheData* Data)
          {
          // your second algorithm goes here
          }
          };

          Using your algorithms looks like this:

          void main()
          {
          IveGotTheData wav;

          if (wav.ReadWaveFile("Test.wav"))
          {
          Algorithm1 alg1;
          Algorithm2 alg2;

            wav.UseMyData(alg1);
            wav.UseMyData(alg2);
          

          }
          }

          Regards Achim Klein


          We can do no great things, only small things with great love. - Mother Theresa

          B 1 Reply Last reply
          0
          • A Achim Klein

            Hi, in my opinion it is always worth an attempt to get around global data. Maybe this design pattern (Visitor) can help you. The class that can read wave files:

            class IveGotTheData
            {
            public:

            // add some 'Getter' methods here

            bool ReadWaveFile(const char* Path);

            void UseMyData(IknowTheAlgorithm& Operator)
            {
            Operator.Operate(this);
            }
            };

            The base class for all algorithms that modify your wave data (the Visitor):

            class IknowTheAlgorithm
            {
            public:

            virtual void Operate(IveGotTheData* Data) = 0;
            };

            Your first algorithm is encapsulated in this class:

            class Algorithm1 : public IknowTheAlgorithm
            {
            public:

            virtual void Operate(IveGotTheData* Data)
            {
            // your first algorithm goes here
            }
            };

            Your second algorithm is encapsulated in this class:

            class Algorithm2 : public IknowTheAlgorithm
            {
            public:

            virtual void Operate(IveGotTheData* Data)
            {
            // your second algorithm goes here
            }
            };

            Using your algorithms looks like this:

            void main()
            {
            IveGotTheData wav;

            if (wav.ReadWaveFile("Test.wav"))
            {
            Algorithm1 alg1;
            Algorithm2 alg2;

              wav.UseMyData(alg1);
              wav.UseMyData(alg2);
            

            }
            }

            Regards Achim Klein


            We can do no great things, only small things with great love. - Mother Theresa

            B Offline
            B Offline
            billiam904
            wrote on last edited by
            #6

            This would be good if it were a new system or code already written in this fashion but I am modifying someone else's code, so I can';t totally rewrite the program. But, thanks. Bill Dennis Orlando, FL

            1 Reply Last reply
            0
            • K khan

              Sorry about that. You need to make the variable public. By default it is private. Just write public: before the variable declaration in the class. Like: class CSomething { public: //this needs to be inserted. static DATA_TYPE m_Data; }; this is this.

              B Offline
              B Offline
              billiam904
              wrote on last edited by
              #7

              I'm sorry but now I get the following message: Single error LNK2001: unresolved external symbol "public: static int CReadDemoWave::testinteger" (?testinteger@CReadDemoWave@@2HA) Here is my .h: #include #include #include #define ERROR_LENGTH 1024 class CReadDemoWave { public: static int testinteger; void ReadDemoWave(); void ReadDemoIntoBuffer(); void PlayWav(); long QueueWaveData(HWAVEOUT HWaveOut, WAVEHDR * waveHeader, DWORD WaveDataPlayed, DWORD WaveBufSize, HMMIO HMmio); LPSTR *GetDemoBuffer(void); void WriteDemo(HMMIO recordHandle); // =========== D == E == M == O ======================= // BD create demo buffer. WAVEHDR lpDemoData; DWORD dwDemoDataSize; int m_nDemoBufferSize; BYTE* m_pbyDemoBuffer; HMMIO m_hmmio; //HMMIO hmmio; char m_szError[ERROR_LENGTH]; int m_nError; MMIOINFO m_mmioInfo; DWORD dwDataSize; DWORD dwCount; HPSTR m_hptr; int m_nBufferSize; }; Bill Dennis Orlando, FL

              A 1 Reply Last reply
              0
              • B billiam904

                I'm sorry but now I get the following message: Single error LNK2001: unresolved external symbol "public: static int CReadDemoWave::testinteger" (?testinteger@CReadDemoWave@@2HA) Here is my .h: #include #include #include #define ERROR_LENGTH 1024 class CReadDemoWave { public: static int testinteger; void ReadDemoWave(); void ReadDemoIntoBuffer(); void PlayWav(); long QueueWaveData(HWAVEOUT HWaveOut, WAVEHDR * waveHeader, DWORD WaveDataPlayed, DWORD WaveBufSize, HMMIO HMmio); LPSTR *GetDemoBuffer(void); void WriteDemo(HMMIO recordHandle); // =========== D == E == M == O ======================= // BD create demo buffer. WAVEHDR lpDemoData; DWORD dwDemoDataSize; int m_nDemoBufferSize; BYTE* m_pbyDemoBuffer; HMMIO m_hmmio; //HMMIO hmmio; char m_szError[ERROR_LENGTH]; int m_nError; MMIOINFO m_mmioInfo; DWORD dwDataSize; DWORD dwCount; HPSTR m_hptr; int m_nBufferSize; }; Bill Dennis Orlando, FL

                A Offline
                A Offline
                Achim Klein
                wrote on last edited by
                #8

                Hi, the problem here is that the definition is missing. Guess your header file (ReadDemoWave.h) looks like this:

                class CReadDemoWave
                {
                public:

                // Construction

                // Getter methods

                // Setter methods

                private:

                // Attributes

                static int m_anyInt; // this is just a declaration
                };

                Then you have to add the definition of m_anyInt to your cpp file:

                #include "ReadDemoWave.h"

                // define your static attributes
                int CReadDemoWave::m_anyInt;

                CReadDemoWave::CReadDemoWave()
                {
                // your constructor code
                }

                // Getter methods

                // Setter methods

                Regards Achim Klein


                We can do no great things, only small things with great love. - Mother Theresa

                1 Reply Last reply
                0
                • B billiam904

                  First, I am new to C++. I have some C background with DOS and Unix but that was 15 years ago. My most recent work was vb.net. I am now doing some audio based coding in VC++ and I am having some problems, probably from my lack of knowledge. Unfortunately, I have until Monday to get this code working... I have three classes and three separate files. The developer that started this is simply calling the classes without reinstantiating them. I am reading a wave file in and that works fine. But, I need to create a global buffer for the wave data so that the other two classes can access it or find someway to pass the buffer by references to the other classes' functions. Can anyone give me some same code on doing this? Thank you. Bill Dennis Orlando, FL

                  A Offline
                  A Offline
                  Achim Klein
                  wrote on last edited by
                  #9

                  Hi, could you post the three header files, please ? Maybe <pre> code </pre> is useful.


                  We can do no great things, only small things with great love. - Mother Theresa

                  B 1 Reply Last reply
                  0
                  • A Achim Klein

                    Hi, could you post the three header files, please ? Maybe <pre> code </pre> is useful.


                    We can do no great things, only small things with great love. - Mother Theresa

                    B Offline
                    B Offline
                    billiam904
                    wrote on last edited by
                    #10

                    ////////////////////////////////////////////////////////////////////// // WavWrite.h: interface for the CWavWrite class. #if !defined(AFX_WAVWRITE_H__2C400A28_DDC3_11D0_9FD3_444553540001__INCLUDED_) #define AFX_WAVWRITE_H__2C400A28_DDC3_11D0_9FD3_444553540001__INCLUDED_ #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000 #include #include #include //#include "readdemowave.h" #define ERROR_LENGTH 1024 //CReadDemoWave cReadDemo; //static DWORD dwDemoDataSize; //WAVEHDR lpDemoData; //static WAVEHDR lpDemoData; /* Definition from MS typedef struct { WORD wFormatTag; WORD nChannels; DWORD nSamplesPerSec; DWORD nAvgBytesPerSec; WORD nBlockAlign; WORD wBitsPerSample; WORD cbSize; } WAVEFORMATEX; typedef struct { FOURCC ckid; DWORD cksize; FOURCC fccType; DWORD dwDataOffset; DWORD dwFlags; } MMCKINFO; */ class CWavWrite { private: char m_szName[FILENAME_MAX]; HMMIO m_hmmioFile; HMMIO m_hmmioDemoFile; MMCKINFO m_ckRIFF, m_ckfmt, m_ckdata; int m_nDataLeft; WAVEFORMATEX *m_pwfx; PCMWAVEFORMAT m_pcmWaveFormat; int m_cbExtraAlloc; MMIOINFO m_mmioInfo; // BD 9/13/05 /*int DEMO; int rndcnt; int bufcnt;*/ // Created here so not created each time Read or Write Data is called // Otherwise these could be local variables - trade speed for size int m_nCopyLength, m_nActualWrite; int m_i; // Error variables int m_nError; char m_szError[ERROR_LENGTH]; public: //WAVEHDR lpDemoData; //DWORD dwDemoDataSize; // Constructor (empty) / Destructor CWavWrite(); virtual ~CWavWrite(); // Data processing - too large to be effective as an inline functions // BD added lat param for demo buffer //int Write(BYTE *pBuffer, int nDataSize, WAVEHDR *pDemoBuffer); int Write(BYTE *pBuffer, int nDataSize); void WriteDemo(BYTE *pBuffer, int nDataSize); // File Manipulation void Open(void); void Init(MMCKINFO *pckRIFF, MMCKINFO *pckfmt, MMCKINFO *pckdata, WAVEFORMATEX *pwfx, int nBufferSize); void Init(short wBitsPerSample, int nSampleFrequency, short wChannels, int nBufferSize); void Close(void); // Parameter Manipulation void SetName(char *); char *GetName(void); // Parameters below may be different than input file, CWavRead. long GetNumberOfSamples(void); long GetSampleFrequency(void); short GetBitsPerSample(void); short GetNumberOfCha

                    A 2 Replies Last reply
                    0
                    • B billiam904

                      ////////////////////////////////////////////////////////////////////// // WavWrite.h: interface for the CWavWrite class. #if !defined(AFX_WAVWRITE_H__2C400A28_DDC3_11D0_9FD3_444553540001__INCLUDED_) #define AFX_WAVWRITE_H__2C400A28_DDC3_11D0_9FD3_444553540001__INCLUDED_ #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000 #include #include #include //#include "readdemowave.h" #define ERROR_LENGTH 1024 //CReadDemoWave cReadDemo; //static DWORD dwDemoDataSize; //WAVEHDR lpDemoData; //static WAVEHDR lpDemoData; /* Definition from MS typedef struct { WORD wFormatTag; WORD nChannels; DWORD nSamplesPerSec; DWORD nAvgBytesPerSec; WORD nBlockAlign; WORD wBitsPerSample; WORD cbSize; } WAVEFORMATEX; typedef struct { FOURCC ckid; DWORD cksize; FOURCC fccType; DWORD dwDataOffset; DWORD dwFlags; } MMCKINFO; */ class CWavWrite { private: char m_szName[FILENAME_MAX]; HMMIO m_hmmioFile; HMMIO m_hmmioDemoFile; MMCKINFO m_ckRIFF, m_ckfmt, m_ckdata; int m_nDataLeft; WAVEFORMATEX *m_pwfx; PCMWAVEFORMAT m_pcmWaveFormat; int m_cbExtraAlloc; MMIOINFO m_mmioInfo; // BD 9/13/05 /*int DEMO; int rndcnt; int bufcnt;*/ // Created here so not created each time Read or Write Data is called // Otherwise these could be local variables - trade speed for size int m_nCopyLength, m_nActualWrite; int m_i; // Error variables int m_nError; char m_szError[ERROR_LENGTH]; public: //WAVEHDR lpDemoData; //DWORD dwDemoDataSize; // Constructor (empty) / Destructor CWavWrite(); virtual ~CWavWrite(); // Data processing - too large to be effective as an inline functions // BD added lat param for demo buffer //int Write(BYTE *pBuffer, int nDataSize, WAVEHDR *pDemoBuffer); int Write(BYTE *pBuffer, int nDataSize); void WriteDemo(BYTE *pBuffer, int nDataSize); // File Manipulation void Open(void); void Init(MMCKINFO *pckRIFF, MMCKINFO *pckfmt, MMCKINFO *pckdata, WAVEFORMATEX *pwfx, int nBufferSize); void Init(short wBitsPerSample, int nSampleFrequency, short wChannels, int nBufferSize); void Close(void); // Parameter Manipulation void SetName(char *); char *GetName(void); // Parameters below may be different than input file, CWavRead. long GetNumberOfSamples(void); long GetSampleFrequency(void); short GetBitsPerSample(void); short GetNumberOfCha

                      A Offline
                      A Offline
                      Achim Klein
                      wrote on last edited by
                      #11

                      Many lines. I need a moment...


                      We can do no great things, only small things with great love. - Mother Theresa

                      1 Reply Last reply
                      0
                      • B billiam904

                        ////////////////////////////////////////////////////////////////////// // WavWrite.h: interface for the CWavWrite class. #if !defined(AFX_WAVWRITE_H__2C400A28_DDC3_11D0_9FD3_444553540001__INCLUDED_) #define AFX_WAVWRITE_H__2C400A28_DDC3_11D0_9FD3_444553540001__INCLUDED_ #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000 #include #include #include //#include "readdemowave.h" #define ERROR_LENGTH 1024 //CReadDemoWave cReadDemo; //static DWORD dwDemoDataSize; //WAVEHDR lpDemoData; //static WAVEHDR lpDemoData; /* Definition from MS typedef struct { WORD wFormatTag; WORD nChannels; DWORD nSamplesPerSec; DWORD nAvgBytesPerSec; WORD nBlockAlign; WORD wBitsPerSample; WORD cbSize; } WAVEFORMATEX; typedef struct { FOURCC ckid; DWORD cksize; FOURCC fccType; DWORD dwDataOffset; DWORD dwFlags; } MMCKINFO; */ class CWavWrite { private: char m_szName[FILENAME_MAX]; HMMIO m_hmmioFile; HMMIO m_hmmioDemoFile; MMCKINFO m_ckRIFF, m_ckfmt, m_ckdata; int m_nDataLeft; WAVEFORMATEX *m_pwfx; PCMWAVEFORMAT m_pcmWaveFormat; int m_cbExtraAlloc; MMIOINFO m_mmioInfo; // BD 9/13/05 /*int DEMO; int rndcnt; int bufcnt;*/ // Created here so not created each time Read or Write Data is called // Otherwise these could be local variables - trade speed for size int m_nCopyLength, m_nActualWrite; int m_i; // Error variables int m_nError; char m_szError[ERROR_LENGTH]; public: //WAVEHDR lpDemoData; //DWORD dwDemoDataSize; // Constructor (empty) / Destructor CWavWrite(); virtual ~CWavWrite(); // Data processing - too large to be effective as an inline functions // BD added lat param for demo buffer //int Write(BYTE *pBuffer, int nDataSize, WAVEHDR *pDemoBuffer); int Write(BYTE *pBuffer, int nDataSize); void WriteDemo(BYTE *pBuffer, int nDataSize); // File Manipulation void Open(void); void Init(MMCKINFO *pckRIFF, MMCKINFO *pckfmt, MMCKINFO *pckdata, WAVEFORMATEX *pwfx, int nBufferSize); void Init(short wBitsPerSample, int nSampleFrequency, short wChannels, int nBufferSize); void Close(void); // Parameter Manipulation void SetName(char *); char *GetName(void); // Parameters below may be different than input file, CWavRead. long GetNumberOfSamples(void); long GetSampleFrequency(void); short GetBitsPerSample(void); short GetNumberOfCha

                        A Offline
                        A Offline
                        Achim Klein
                        wrote on last edited by
                        #12

                        I guess you have a class named CWavRead. I guess you want to pass the buffer that this class fills to other functions. I guess all of this takes place somewhere inside your CSingleDlg class. Please correct me, if I'm wrong. In this case my header file would approximately look like this:

                        // forward declaration
                        class CWavRead;

                        // class definition
                        class CSingleDlg
                        {
                        public:

                        ...

                        private:

                        // my dialog 'knows' an object of the CWavRead class
                        CWavRead* m_wavReader;
                        };

                        And my implementation file would approximately look like this:

                        #include "SingleDlg.h"
                        #include "WavRead.h"

                        // constructor
                        CSingleDlg::CSingleDlg()
                        {
                        m_wavReader = new CWavRead;
                        }

                        // destructor
                        CSingleDlg::~CSingleDlg()
                        {
                        delete m_wavReader;
                        }

                        // read wav file
                        CSingleDlg::OnFileOpen()
                        {
                        m_wavReader->read(path);
                        }

                        // modify wav data
                        CSingleDlg::OnEditModify()
                        {
                        // get data
                        long size = m_wavReader->getBufferSize();
                        BYTE* buffer = m_wavReader->getBuffer();

                        // create local (temporary) object
                        CWavOperator operator;

                        // pass the read data
                        operator.DoSomething(buffer, size);
                        }

                        Is it useful ?


                        We can do no great things, only small things with great love. - Mother Theresa

                        A 1 Reply Last reply
                        0
                        • A Achim Klein

                          I guess you have a class named CWavRead. I guess you want to pass the buffer that this class fills to other functions. I guess all of this takes place somewhere inside your CSingleDlg class. Please correct me, if I'm wrong. In this case my header file would approximately look like this:

                          // forward declaration
                          class CWavRead;

                          // class definition
                          class CSingleDlg
                          {
                          public:

                          ...

                          private:

                          // my dialog 'knows' an object of the CWavRead class
                          CWavRead* m_wavReader;
                          };

                          And my implementation file would approximately look like this:

                          #include "SingleDlg.h"
                          #include "WavRead.h"

                          // constructor
                          CSingleDlg::CSingleDlg()
                          {
                          m_wavReader = new CWavRead;
                          }

                          // destructor
                          CSingleDlg::~CSingleDlg()
                          {
                          delete m_wavReader;
                          }

                          // read wav file
                          CSingleDlg::OnFileOpen()
                          {
                          m_wavReader->read(path);
                          }

                          // modify wav data
                          CSingleDlg::OnEditModify()
                          {
                          // get data
                          long size = m_wavReader->getBufferSize();
                          BYTE* buffer = m_wavReader->getBuffer();

                          // create local (temporary) object
                          CWavOperator operator;

                          // pass the read data
                          operator.DoSomething(buffer, size);
                          }

                          Is it useful ?


                          We can do no great things, only small things with great love. - Mother Theresa

                          A Offline
                          A Offline
                          Achim Klein
                          wrote on last edited by
                          #13

                          Unfortunately I don't know the interface of your CWavRead class...


                          We can do no great things, only small things with great love. - Mother Theresa

                          B 1 Reply Last reply
                          0
                          • A Achim Klein

                            Unfortunately I don't know the interface of your CWavRead class...


                            We can do no great things, only small things with great love. - Mother Theresa

                            B Offline
                            B Offline
                            billiam904
                            wrote on last edited by
                            #14

                            Thanks. Can you give me an idea of the implementation code for the getBuffer function to make sure I'm doing it right. I wrote one but it kepts resetting. Here's my read .h #include #include #include #define ERROR_LENGTH 1024 class CReadDemoWave { public: BYTE m_pbyDemoBuffer; static int testinteger; void ReadDemoWave(); BYTE *ReadDemoIntoBuffer(); void PlayWav(); long QueueWaveData(HWAVEOUT HWaveOut, WAVEHDR * waveHeader, DWORD WaveDataPlayed, DWORD WaveBufSize, HMMIO HMmio); BYTE *GetDemoBuffer(void); //void WriteDemo(HMMIO recordHandle); //public: // =========== D == E == M == O ======================= // BD create demo buffer. WAVEHDR lpDemoData; DWORD dwDemoDataSize; int m_nDemoBufferSize; //BYTE* m_pbyDemoBuffer; HMMIO m_hmmio; //HMMIO hmmio; char m_szError[ERROR_LENGTH]; int m_nError; MMIOINFO m_mmioInfo; DWORD dwDataSize; DWORD dwCount; HPSTR m_hptr; int m_nBufferSize; //WAVEFORMATEX waveFormat; }; Bill Dennis Orlando, FL

                            A 1 Reply Last reply
                            0
                            • B billiam904

                              Thanks. Can you give me an idea of the implementation code for the getBuffer function to make sure I'm doing it right. I wrote one but it kepts resetting. Here's my read .h #include #include #include #define ERROR_LENGTH 1024 class CReadDemoWave { public: BYTE m_pbyDemoBuffer; static int testinteger; void ReadDemoWave(); BYTE *ReadDemoIntoBuffer(); void PlayWav(); long QueueWaveData(HWAVEOUT HWaveOut, WAVEHDR * waveHeader, DWORD WaveDataPlayed, DWORD WaveBufSize, HMMIO HMmio); BYTE *GetDemoBuffer(void); //void WriteDemo(HMMIO recordHandle); //public: // =========== D == E == M == O ======================= // BD create demo buffer. WAVEHDR lpDemoData; DWORD dwDemoDataSize; int m_nDemoBufferSize; //BYTE* m_pbyDemoBuffer; HMMIO m_hmmio; //HMMIO hmmio; char m_szError[ERROR_LENGTH]; int m_nError; MMIOINFO m_mmioInfo; DWORD dwDataSize; DWORD dwCount; HPSTR m_hptr; int m_nBufferSize; //WAVEFORMATEX waveFormat; }; Bill Dennis Orlando, FL

                              A Offline
                              A Offline
                              Achim Klein
                              wrote on last edited by
                              #15

                              If your read function fills m_pbyDemoBuffer, just write

                              BYTE* CReadDemoWave::getBuffer() const
                              {
                              return m_pbyDemoBuffer;
                              }

                              Returning a reference (pointer) breaks the privacy concept of your class, but it's handy.


                              We can do no great things, only small things with great love. - Mother Theresa

                              B 1 Reply Last reply
                              0
                              • A Achim Klein

                                If your read function fills m_pbyDemoBuffer, just write

                                BYTE* CReadDemoWave::getBuffer() const
                                {
                                return m_pbyDemoBuffer;
                                }

                                Returning a reference (pointer) breaks the privacy concept of your class, but it's handy.


                                We can do no great things, only small things with great love. - Mother Theresa

                                B Offline
                                B Offline
                                billiam904
                                wrote on last edited by
                                #16

                                I have it like this: BYTE *CReadDemoWave::GetDemoBuffer(void) { return &m_pbyDemoBuffer; } When I take out the refrence (&), I get a compiler error saying it can't convert from BYTE to BYTE*. It's been 15 years since I've worked with pointers so not sure I am doing this correctly. My pointer seems to be resetting itself from the time the buffer is created and the time I call GetDemoBuffer(). Bill Dennis Orlando, FL

                                A 1 Reply Last reply
                                0
                                • B billiam904

                                  I have it like this: BYTE *CReadDemoWave::GetDemoBuffer(void) { return &m_pbyDemoBuffer; } When I take out the refrence (&), I get a compiler error saying it can't convert from BYTE to BYTE*. It's been 15 years since I've worked with pointers so not sure I am doing this correctly. My pointer seems to be resetting itself from the time the buffer is created and the time I call GetDemoBuffer(). Bill Dennis Orlando, FL

                                  A Offline
                                  A Offline
                                  Achim Klein
                                  wrote on last edited by
                                  #17

                                  BYTE m_pbyDemoBuffer is no array. It's just one number. If you want to create an array, please use new and delete:

                                  int size = 1000;

                                  // allocate memory
                                  BYTE* array = new BYTE[size];

                                  // free memory
                                  delete[] array;

                                  It's like malloc and free.


                                  We can do no great things, only small things with great love. - Mother Theresa

                                  B 1 Reply Last reply
                                  0
                                  • A Achim Klein

                                    BYTE m_pbyDemoBuffer is no array. It's just one number. If you want to create an array, please use new and delete:

                                    int size = 1000;

                                    // allocate memory
                                    BYTE* array = new BYTE[size];

                                    // free memory
                                    delete[] array;

                                    It's like malloc and free.


                                    We can do no great things, only small things with great love. - Mother Theresa

                                    B Offline
                                    B Offline
                                    billiam904
                                    wrote on last edited by
                                    #18

                                    In another post, I asked someone to show me how to allocate member for m_pbyDemoBuffer. I am rewading the wave data into a LPSTR variable, then creating and allocating memory for m_pbyDemoBuffer using GlobalAlloc and doing memcpy from lpDemoData.lpData to m_pbyDemoBuffer. I am then returning *m_pbyDemoBuffer in GetDemoBuffer(). So, not using arrays here. Bill Dennis Orlando, FL

                                    A 1 Reply Last reply
                                    0
                                    • B billiam904

                                      In another post, I asked someone to show me how to allocate member for m_pbyDemoBuffer. I am rewading the wave data into a LPSTR variable, then creating and allocating memory for m_pbyDemoBuffer using GlobalAlloc and doing memcpy from lpDemoData.lpData to m_pbyDemoBuffer. I am then returning *m_pbyDemoBuffer in GetDemoBuffer(). So, not using arrays here. Bill Dennis Orlando, FL

                                      A Offline
                                      A Offline
                                      Achim Klein
                                      wrote on last edited by
                                      #19

                                      I am reading the wave data into a LPSTR variable, [...] How do you allocate the memory at the position your LPSTR variable points to ?


                                      We can do no great things, only small things with great love. - Mother Theresa

                                      B 1 Reply Last reply
                                      0
                                      • A Achim Klein

                                        I am reading the wave data into a LPSTR variable, [...] How do you allocate the memory at the position your LPSTR variable points to ?


                                        We can do no great things, only small things with great love. - Mother Theresa

                                        B Offline
                                        B Offline
                                        billiam904
                                        wrote on last edited by
                                        #20

                                        I've pasted part of the code below. I have a lot of junk commented out in the actual .cpp file but if you want that, I can send it without violating my contract. Thanks. ''Allocate space for data in lpDemoData structure. // lpData is LPSTR. if (!( lpDemoData.lpData = (char *)VirtualAlloc(0, dwDemoDataSize<<1, MEM_COMMIT, PAGE_READWRITE))) { printf("ERROR: Can't allocate memory for WAVE buffer!\n"); } // Read Data Chunk into lpData if (readres = mmioRead(hmmio, (HPSTR )lpDemoData.lpData, dwDemoDataSize) != dwDemoDataSize) { AfxMessageBox("Error: Failed to read data chunk."); mmioClose(hmmio,0); return; } //Create byte-based buffer, allocate memory and copy // lpData to it. int lenlpData=strlen(lpDemoData.lpData); m_pbyDemoBuffer=(BYTE)GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, lenlpData ); memcpy((BYTE*)m_pbyDemoBuffer, lpDemoData.lpData, lenlpData); Bill Dennis Orlando, FL

                                        A 1 Reply Last reply
                                        0
                                        • B billiam904

                                          I've pasted part of the code below. I have a lot of junk commented out in the actual .cpp file but if you want that, I can send it without violating my contract. Thanks. ''Allocate space for data in lpDemoData structure. // lpData is LPSTR. if (!( lpDemoData.lpData = (char *)VirtualAlloc(0, dwDemoDataSize<<1, MEM_COMMIT, PAGE_READWRITE))) { printf("ERROR: Can't allocate memory for WAVE buffer!\n"); } // Read Data Chunk into lpData if (readres = mmioRead(hmmio, (HPSTR )lpDemoData.lpData, dwDemoDataSize) != dwDemoDataSize) { AfxMessageBox("Error: Failed to read data chunk."); mmioClose(hmmio,0); return; } //Create byte-based buffer, allocate memory and copy // lpData to it. int lenlpData=strlen(lpDemoData.lpData); m_pbyDemoBuffer=(BYTE)GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, lenlpData ); memcpy((BYTE*)m_pbyDemoBuffer, lpDemoData.lpData, lenlpData); Bill Dennis Orlando, FL

                                          A Offline
                                          A Offline
                                          Achim Klein
                                          wrote on last edited by
                                          #21

                                          Yeah, if your project isn't yet working, I'll have a look at it.


                                          We can do no great things, only small things with great love. - Mother Theresa

                                          B 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups