CANoe DLL programming - generate SendKey.dll through VS

Related articles CANoe DLL programming (I) -- creating DLL and dynamic calling by Visual Studio CANoe DLL programming (...
GenerateKeyEx interface
GenerateKeyExOpt interface
Rich functions in the source code
Related articles

CANoe DLL programming (I) -- creating DLL and dynamic calling by Visual Studio

CANoe DLL programming (II) -- create and call the DLL applicable to CANoe

CANoe DLL programming (III) -- DLL and callback function
CANoe DLL programming (IV) -- Application of SendKey DLL

preface
  • In the previous section, we have understood the diagnosis mechanism of seedkey. In this section, we will continue to see how to generate seedkey.dll with vs.

  • Software environment:
    win10 x64
    visual studio 2019
    CANoe 11 x64

Interpretation of official examples

GenerateKeyEx interface

① , CANoe provides two function interfaces

  • The following screenshot means that the DLL looks for the API interface. GenerateKeyExOpt and GenerateKeyEx find which one to use first.

② , two VS projects are provided in the official project, corresponding to the above two functions. The seedkey.dll used in the project actually uses KeyGenDll_GenerateKeyEx

③ , let's open it with VS. look at the source code. It's very simple. There are only a few lines of code in the function`

  • When the array size of the key is smaller than that of the seed, an exception is thrown
  • Then, the seed is reversed and returned as the key value

  • The following is the trace screenshot of seed key. You can see that the algorithm is indeed negative.

GenerateKeyExOpt interface

① , we put the jVS project keygendll_ Make a copy of GenerateKeyEx, and then in the copied project, change the source code from GenerateKeyEx interface to GenerateKeyExOpt according to the following figure, add ipOptions parameter, and then regenerate a seedkey.dll

  • Here, for the sake of distinction, I change the name of the generated DLL to SeednKey_Opt.dll

② , set the calling DLL to SeednKey_Opt.dll, then save, close CANoe, and restart CANoe

  • The reason for turning off and restarting is that if you do not turn off and restart, the dll call path printed on the diagnostic console will remain unchanged, although it has changed internally. In order to make an accurate explanation and avoid misunderstanding, you'd better turn off and restart.

③ Then we send 27 services on the diagnostic console and observe the normal unlocking.

  • We can also observe the calling path of dll here. If CANoe is not restarted, the path of the previous dll is still displayed here, which may be a bug in CANoe 11 SP2

Take a minute off...

Rich functions in the source code

① In real projects, different seed key level s have different complex algorithms. In the official project, level 1 and level 11 are supported. Now we add a function, level 1, to get the key value by taking the inverse; Level 11, + 1 gets the key value; otherwise, an error is returned.

KEYGENALGO_API VKeyGenResultEx GenerateKeyExOpt( const unsigned char* iSeedArray, /* Array for the seed [in] */ unsigned int iSeedArraySize, /* Length of the array for the seed [in] */ const unsigned int iSecurityLevel, /* Security level [in] */ const char* iVariant, /* Name of the active variant [in] */ const char* ipOptions, unsigned char* ioKeyArray, /* Array for the key [in, out] */ unsigned int iKeyArraySize, /* Maximum length of the array for the key [in] */ unsigned int& oSize /* Length of the key [out] */ ) { if (iSeedArraySize>iKeyArraySize) return KGRE_BufferToSmall; if (iSecurityLevel == 0x01) { for (unsigned int i = 0; i < iSeedArraySize; i++) ioKeyArray[i] = ~iSeedArray[i]; } else if (iSecurityLevel == 0x11) { for (unsigned int i = 0; i < iSeedArraySize; i++) ioKeyArray[i] = iSeedArray[i]+1; } else { return KGRE_SecurityLevelInvalid; } oSize = iSeedArraySize; return KGRE_Ok; }

② , load the regenerated DLL, and then test to see the results. See that the algorithm of Level 11 has changed.

summary

This series demonstrates the source code used

  • To have the most simple life, the most distant dream, even if it is cold tomorrow, the road is far away and the horse is dead!
  • If this blog is helpful to you, please click "like", "comment" and "collect" for three times! The code word is not easy. Your support is the driving force for me to stick to it.

12 November 2021, 11:38 | Views: 6651

Add new comment

For adding a comment, please log in
or create account

0 comments