Attach Object to Skeletal Mesh Socket C++

Hi guys!

I am kind of stuck at a problem I ran into. I want to attach my Weapon Blueprint to the Character Socket.
.cpp

	const USkeletalMeshSocket* weaponSocket;
	weaponSocket = GetMesh()->GetSocketByName(FName("WeaponSocket"));
	FName fnWeaponSocket = weaponSocket->GetFName();

	Debug::Log(fnWeaponSocket.ToString());

	m_cUClassWeapon = StaticLoadClass(UObject::StaticClass(), nullptr, TEXT("/Game/Blueprints/CharacterController/BP_Pistol.BP_Pistol_C"));
	m_cWeapon = GetWorld()->SpawnActor<AWeapon>(m_cUClassWeapon, spawnParams);
	m_cWeapon->AttachRootComponentToActor(this, fnWeaponSocket, EAttachLocation::SnapToTarget, true);

The problem is that Unreal obviously does not find the Socket which I attached to the Skeletal Mesh in Editor.

The engine always attaches the weapon to the center of my character.

When I print the Variable on Screen in Line 5 I get the Name “SkeletalMeshSocket_0”, which is obviously not the name I gave the socket but seems to me like some kind of intern name?
What really bugs me is the fact, that when I build the same structure with blueprints it works perfectly and the weapon snaps to the selected socket.

So I just want to know, did I miss something in code or is this some unreal related bug?

Thanks for helping!

I just use the actual name when attaching to a socket, i.e.

FName weaponSocketName = TEXT("WeaponSocket");

m_cWeapon->AttachRootComponentToActor(this, weaponSocketName, EAttachLocation::SnapToTarget, true);

That was my first attemp in doing this and it didn’t work. Its the reason why I tried to get the socket name with GetSocketByName().

Thanks for the advice. That was my first attempt in doing this and it didnt work, so I tried solving it with GetSocketByName()

this should work

FName fnWeaponSocket = TEXT("WeaponSocket");
m_cUClassWeapon = StaticLoadClass(UObject::StaticClass(), nullptr, TEXT("/Game/Blueprints/CharacterController/BP_Pistol.BP_Pistol_C"));
m_cWeapon = GetWorld()->SpawnActor<AWeapon>(m_cUClassWeapon, spawnParams);
m_cWeapon->AttachRootComponentTo(GetMesh(), fnWeaponSocket, EAttachLocation::SnapToTarget, true);

look at the 4th line

Oh, I am such a blind fool! Thank you so much, worked perfectly!

Well spotted on the GetMesh().