Skip to content

Commit

Permalink
Merge pull request #142 from K-Society/develop
Browse files Browse the repository at this point in the history
Develop
* Fix loaderPath in dllMain.cpp, now it works even if you use the library in a windows service.
  • Loading branch information
maniglia authored May 16, 2024
2 parents aae7ac6 + dabdd26 commit 2afbd01
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ Visual Studio 2022/
Visual Studio 2022Templates/
Test/
src/01/Samples/Dojo/
src/01/Samples/DFU/
src/01/KSociety.ProgrammerTerminal
KSociety.Dojo.sln
KSociety.ProgrammerTerminal.sln
1 change: 0 additions & 1 deletion KSociety.SharpCubeProgrammer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ Global
{8709E86A-F53A-4CED-B485-5D8A40FE9CCA}.Debug|Any CPU.ActiveCfg = Debug|x64
{8709E86A-F53A-4CED-B485-5D8A40FE9CCA}.Debug|Any CPU.Build.0 = Debug|x64
{8709E86A-F53A-4CED-B485-5D8A40FE9CCA}.Debug|x64.ActiveCfg = Debug|x64
{8709E86A-F53A-4CED-B485-5D8A40FE9CCA}.Debug|x64.Build.0 = Debug|x64
{8709E86A-F53A-4CED-B485-5D8A40FE9CCA}.Debug|x86.ActiveCfg = Debug|Win32
{8709E86A-F53A-4CED-B485-5D8A40FE9CCA}.Debug|x86.Build.0 = Debug|Win32
{8709E86A-F53A-4CED-B485-5D8A40FE9CCA}.Release|Any CPU.ActiveCfg = Release|Win32
Expand Down
13 changes: 12 additions & 1 deletion src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace SharpCubeProgrammer
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -53,7 +54,9 @@ private void Init()
{
if (this._handle == null)
{
this._handle = Native.Utility.LoadNativeLibrary(Environment.Is64BitProcess ? @".\dll\x64\STLinkUSBDriver.dll" : @".\dll\x86\STLinkUSBDriver.dll", IntPtr.Zero, 0);
var currentDirectory = GetAssemblyDirectory();

this._handle = Native.Utility.LoadNativeLibrary(Environment.Is64BitProcess ? currentDirectory + @".\dll\x64\STLinkUSBDriver.dll" : currentDirectory + @".\dll\x86\STLinkUSBDriver.dll", IntPtr.Zero, 0);

if (this._handle.IsInvalid)
{
Expand All @@ -71,6 +74,14 @@ private void Init()
}
}

private static string GetAssemblyDirectory()
{
var codeBase = Assembly.GetExecutingAssembly().Location;
var uri = new UriBuilder(codeBase);
var path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}

#region [ST-LINK]

//ST-LINK module groups debug ports JTAG/SWD functions together.
Expand Down
4 changes: 2 additions & 2 deletions src/01/KSociety.Test/KSociety.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PackageReference Include="xunit" Version="2.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
7 changes: 4 additions & 3 deletions src/01/Programmer/Programmer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalLibraryDirectories>$(SolutionDir)Programmer\$(PlatformTarget);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>CubeProgrammer_API.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>vcruntimed.lib;ucrtd.lib;CubeProgrammer_API.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ImageHasSafeExceptionHandlers />
</Link>
<ProjectReference>
Expand Down Expand Up @@ -149,13 +149,14 @@
<Optimization>MaxSpeed</Optimization>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<IntrinsicFunctions />
<TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalLibraryDirectories>$(SolutionDir)Programmer\$(PlatformTarget);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>CubeProgrammer_API.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>vcruntimed.lib;ucrtd.lib;CubeProgrammer_API.lib;%(AdditionalDependencies)</AdditionalDependencies>
<DelayLoadDLLs>
</DelayLoadDLLs>
</Link>
Expand Down Expand Up @@ -225,4 +226,4 @@
<Error Condition="!Exists('..\..\..\packages\Nerdbank.GitVersioning.3.6.133\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\packages\Nerdbank.GitVersioning.3.6.133\build\Nerdbank.GitVersioning.targets'))" />
<Error Condition="!Exists('..\..\..\packages\Microsoft.Build.Tasks.Git.8.0.0\build\Microsoft.Build.Tasks.Git.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\packages\Microsoft.Build.Tasks.Git.8.0.0\build\Microsoft.Build.Tasks.Git.props'))" />
</Target>
</Project>
</Project>
43 changes: 38 additions & 5 deletions src/01/Programmer/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"

const char* loaderPath = "./st/Programmer";
const wchar_t* loaderPath = L"st\\Programmer";

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
UNREFERENCED_PARAMETER(lpReserved);

WCHAR dllName[MAX_PATH + 1];
DWORD size = 0;
int result = 0;

switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
/* Set device loaders path that contains FlashLoader and ExternalLoader folders*/
SetLoadersPath(loaderPath);

size = GetModuleFileNameW(hModule, dllName, MAX_PATH);
if (size > 0)
{
std::wstring dllNameStr(dllName);
std::size_t pos = dllNameStr.size() - 22;
dllNameStr = dllNameStr.substr(0, pos);
dllNameStr += loaderPath;
std::replace(dllNameStr.begin(), dllNameStr.end(), '\\', '/');
const wchar_t* input = dllNameStr.c_str();

// Count required buffer size (plus one for null-terminator).
size_t bufferSize = (wcslen(input) + 1) * sizeof(wchar_t);
char* buffer = new char[bufferSize];

size_t convertedSize;
wcstombs_s(&convertedSize, buffer, bufferSize, input, bufferSize);

// Set device loaders path that contains FlashLoader and ExternalLoader folders.
SetLoadersPath(buffer);

delete[] buffer;

result = 1;
}
break;

case DLL_THREAD_ATTACH:
Expand All @@ -25,6 +54,10 @@ BOOL APIENTRY DllMain( HMODULE hModule,
break;
}

return TRUE;
}
if (result == 1)
{
return TRUE;
}

return FALSE;
}
4 changes: 4 additions & 0 deletions src/01/Programmer/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
// add headers that you want to pre-compile here
#include "framework.h"
#include "Programmer.h"
#include <string>
#include <tchar.h>
#include <stdio.h>
#include <algorithm>

#endif //PCH_H
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
// ProgrammerConsoleApplication.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <iostream>
#include "../Programmer/Programmer.h"
#include <stdlib.h>
#include <algorithm>
//#include "../Programmer/Programmer.h"

const wchar_t* wide = L"zyxw\\..\\..\\pippo";

int main()
{
std::cout << "Hello World!\n";
WCHAR dllName[MAX_PATH + 1];
DWORD size = GetModuleFileNameW(NULL, dllName, MAX_PATH);

//std::wstring s(dllName);

//s += std::wstring(ws2);

std::wstring s(dllName);
std::size_t pos = s.size() - 40;
s = s.substr(0, pos);
s += wide;

std::replace(s.begin(), s.end(), '\\', '/');
const wchar_t* input = s.c_str();

// Count required buffer size (plus one for null-terminator).
size_t size1 = (wcslen(input) + 1) * sizeof(wchar_t);
char* buffer = new char[size1];

#ifdef __STDC_LIB_EXT1__
// wcstombs_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined
size_t convertedSize;
std::wcstombs_s(&convertedSize, buffer, size, input, size);
#else
//std::wcstombs(buffer, input, size1);
#endif

size_t convertedSize;
wcstombs_s(&convertedSize, buffer, size1, input, size1);
delete[] buffer;
;
/*std::cout << "Hello World!\n";
debugConnectParameters* stLinkList;
debugConnectParameters debugParameters;
generalInf* genInfo;
Expand All @@ -32,47 +70,47 @@ int main()
}
std::cout << "-----------------------------------------------\n\n";
}
}*/

for (int index = 0; index < getStlinkListNb; index++) {
//for (int index = 0; index < getStlinkListNb; index++) {

//logMessage(Title, "\n--------------------- ");
//logMessage(Title, "\n ST-LINK Probe : %d ", index);
//logMessage(Title, "\n--------------------- \n\n");

debugParameters = stLinkList[index];
/*debugParameters = stLinkList[index];
debugParameters.connectionMode = UNDER_RESET_MODE;
debugParameters.shared = 0;
debugParameters.shared = 0;*/
//debugParameters.speed = 1;

/* Target connect */
int connectStlinkFlag = ConnectStLink(debugParameters);
/*int connectStlinkFlag = ConnectStLink(debugParameters);
if (connectStlinkFlag != 0) {
std::cout << "Establishing connection with the device failed: " << connectStlinkFlag << " \n" << std::endl;
Disconnect();
continue;
}
else {
std::cout << "\n--- Device Connected --- \n" ;
}
}*/

/* Display device informations */
genInfo = GetDeviceGeneralInf();
/*genInfo = GetDeviceGeneralInf();
std::cout << "\nDevice name: " << genInfo->name << std::endl;
std::cout << "\nDevice type: " << genInfo->type << std::endl;
std::cout << "\nDevice CPU : " << genInfo->cpu << std::endl;
std::cout << "\nDevice CPU : " << genInfo->cpu << std::endl;*/

/* Read Option bytes from target device memory */
peripheral_C* ob;
/*peripheral_C* ob;
ob = InitOptionBytesInterface();
if (ob == 0)
{
Disconnect();
continue;
}
}*/

/* Display option bytes */
for (unsigned int i = 0; i < ob->banksNbr; i++)
/*for (unsigned int i = 0; i < ob->banksNbr; i++)
{
std::cout << "\nOPTION BYTES BANK: " << i << std::endl;
for (unsigned int j = 0; j < ob->banks[i]->categoriesNbr; j++)
Expand All @@ -88,10 +126,10 @@ int main()
}
}
}
}
}
}*/
//}

Disconnect();
//Disconnect();

return 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,6 @@
<ItemGroup>
<ClCompile Include="ProgrammerConsoleApplication.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Programmer\Programmer.vcxproj">
<Project>{52455efe-41b6-4c8b-97c0-db796fe725b5}</Project>
<CopyLocalSatelliteAssemblies>false</CopyLocalSatelliteAssemblies>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/01/Samples/QuickStart/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private static void Main(string[] args)
Console.WriteLine("Press a button to exit.");
Console.ReadLine();
}

private static void ReceiveMessage(int messageType, [MarshalAs(UnmanagedType.LPWStr)] string message)
{
message = Regex.Replace(message, "(?<!\r)\n", "");
Expand Down

0 comments on commit 2afbd01

Please sign in to comment.